Reading XML file from HTTP

AitorEGAitorEG Member Posts: 342
edited 2017-12-18 in NAV Three Tier
Hi everyone,
I'm finding an issue with the objects involving ain a routine i am cerating for reading an XMl file from an HTTP request. here is the part of the code:
IF ISCLEAR(locautXmlDoc) THEN
   CREATE(locautXmlDoc,FALSE,TRUE);
locautXmlDoc.load(XMLHTTP.responseXML);
CREATE(xmlNodeList, FALSE, TRUE);
xmlNodeList := locautXmlDoc.getElementsByTagName('CONNUMBER');

The code fails when it tries to create the xmlNodeList variable, with this error:

6fuc8mhdsliy.png
Cannot create an instance for the automation server with CLSIF ={XXX-XXXX}

I have to say that the HTTP call has a response, that can be seen with the Fiddler (courtesy of @Slawek_Guzek )
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><document><CREATE><CONREF>15D</CONREF><CONNUMBER>GE158487071ES</CONNUMBER><SUCCESS>Y</SUCCESS></CREATE></document>

What I'm doing wrong in the creation of the NodeList?
Thank you very much

Best Answer

Answers

  • AitorEGAitorEG Member Posts: 342
    Now, I'm Trying to do this (without doing the CREATE for the nodelist):
    locautXmlDoc.load(XMLHTTP.responseXML);
    xmlNodeList := locautXmlDoc.getElementsByTagName('CONNUMBER');
    xmldomElem1 := xmlNodeList.item(0);
    conNote := xmldomElem1.text();
    

    And I get this error:

    5ccjhao9b8dt.png

    Interrupt in error message:
    The instance for the automation variable has not been created
    
  • AdrianAkersAdrianAkers Member Posts: 137
    You can’t create automation variable on the server. You must create it client side or use .NET library server side.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Look into Codeunit 6224 XML DOM Management. You will find there a bunch of functions dealing with XML, files all .NET based.

    Use these functions/variables in your solution.

    For example, rather than
    IF ISCLEAR(locautXmlDoc) THEN
       CREATE(locautXmlDoc,FALSE,TRUE);
    locautXmluDoc.load(XMLHTTP.responseXML);
    
    try
    IF NOT C6224.LoadXMLDocumentFromText(XMLHTTP.responseXML, XMLDocument) THEN 
      HandleError()...
    
    The call wll return the XMLDocument var, which is a .NET type XML object
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • AitorEGAitorEG Member Posts: 342
    You can’t create automation variable on the server. You must create it client side or use .NET library server side.

    I've already created some automation variables like this, changing the "RunOnClient" paremeter to "YES", but in this case, It's not posible
  • AitorEGAitorEG Member Posts: 342
    edited 2017-12-19
    Look into Codeunit 6224 XML DOM Management. You will find there a bunch of functions dealing with XML, files all .NET based.

    Use these functions/variables in your solution.

    For example, rather than
    IF ISCLEAR(locautXmlDoc) THEN
       CREATE(locautXmlDoc,FALSE,TRUE);
    locautXmluDoc.load(XMLHTTP.responseXML);
    
    try
    IF NOT C6224.LoadXMLDocumentFromText(XMLHTTP.responseXML, XMLDocument) THEN 
      HandleError()...
    
    The call wll return the XMLDocument var, which is a .NET type XML object

    Thanks, I'll try with this approach, Really appreciated!!!

    edit: In this sentence, I have a compilation error:
    XMLDomMgmt.LoadXMLDocumentFromText(XMLHTTP.responseXML, locautXmlDoc);
    

    locautXmlDoc DotNet System.Xml.XmlDocument.'System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    n1we4vjimd1v.png

    "Cannot convert type because one of the operators has an invalid type
    Text := Automation"

    Which type must the objetcs be?

  • AitorEGAitorEG Member Posts: 342
    "XMLHTTP.responseText" returns correctly the response value of the request, I can confirm that, The issue is to load it on an XML file, to later get the value of the required node.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690

    LoadXMLDocumentFromText accepts Text in the first parameter, so perhaps you should do this call in 2 steps:
    ResponseXmlText := XMLHTTP.responseText;
    IF NOT C6224.LoadXMLDocumentFromText(ResponseXmlText , XMLDocument) THEN 
      HandleError()...
    
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • AitorEGAitorEG Member Posts: 342
    LoadXMLDocumentFromText accepts Text in the first parameter, so perhaps you should do this call in 2 steps:
    ResponseXmlText := XMLHTTP.responseText;
    IF NOT C6224.LoadXMLDocumentFromText(ResponseXmlText , XMLDocument) THEN 
      HandleError()...
    

    Thanks, I've tried that, But now, my error is when tryng to get the elemnt from the XML. I'm doing this:
    xmlNodeList := locautXmlDoc.GetElementsByTagName('CONNUMBER');
    xmldomElem1 := xmlNodeList.item(0);
    conNote := xmldomElem1.text();
    

    But it says that assignment is not allowed for "xmlNodeList ". Should I change the type of the variable? What types are the bests for doin g this issue? I'm getting a mess whit DOMDocument, XMLDocuments....

  • AitorEGAitorEG Member Posts: 342
    I'm using the FindNodeTExt function:
    ResponseText := XMLHTTP.responseText;
    XMLDomMgmt.LoadXMLDocumentFromText(ResponseText, locautXmlDoc);
    conNote := XMLDomMgmt.FindNodeText(locautXmlDoc,'//DOCUMENT/CREATE/CONNUMBER');
    

    But when I run the app, I see the error:

    bt8nm9am5xt7.png
    Cannot serialize an instance of the .Net framework object: ........
    

    I've found this type of erros quite a lot while developing this application, and I fixed it changing the "RunOnClient" parameter on the DotNet variables. But now it's not working. When debbuging, I can see this:


    hnpqahmn56qk.png

    It is failing in the 6224 CU. I've generated and made the HTTP request with Automation instead of DOTNet variables because of this issue
    How can this problem be solved?
  • AitorEGAitorEG Member Posts: 342
    Solved! i had to change all the RunOnClient propertis, not just in the 6224CU, but also in my develepment, where I forgot one of them.Thanks!
Sign In or Register to comment.