Options

I need help parsing this string

JLeePlnrJLeePlnr Member Posts: 26
Hi guys,
I need help in parsing the below mentioned string. This string is a .xml response from the back.

<?xml version="1.0"?>
<ncresponse NCERRORPLUS="This order has already been processed." ALIAS="ABC33557302" BRAND="American Express" PM="CreditCard" currency="GBP" amount="372.76" STATUS="0" ACCEPTANCE="test123456" NCERROR="50001113" NCSTATUS="0" PAYID="3035462606" orderID="2373653218"> </ncresponse>

I want to load this .xml file and capture the values of NCERRORPLUS, NCERROR, and NCSTATUS. I am going to use the return values on these fields to further control the sales order processing with credit cards.

Please help.

Thanks NAV gurus!!!!

Comments

  • Options
    Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-05-21
    Look into Codeunit 6224 XML DOM Management, you will find there a few functions which will help you to extract required attributes:
    c6224.LoadXMLDocumentFromText(XMLDoc,...)
    IF c6224.FindNode(XMLDoc, '/ncresponse/@NCERRORPLUS', XMLNode) THEN
      NCERRORPLUSValue = XMLNode.InnerText;
    IF c6224.FindNode(XMLDoc, '/ncresponse/@NCERROR', XMLNode) THEN
      NCERRORValue = XMLNode.InnerText;
    

    You will find all required variable types in the codeunit too (in function parameters definitions)
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    edited 2018-05-23
    Look into Codeunit 6224 XML DOM Management, you will find there a few functions which will help you to extract required attributes:
    c6224.LoadXMLDocumentFromText(XMLDoc,...)
    IF c6224.FindNode(XMLDoc, '/ncresponse/@NCERRORPLUS', XMLNode) THEN
      NCERRORPLUSValue = XMLNode.InnerText;
    IF c6224.FindNode(XMLDoc, '/ncresponse/@NCERROR', XMLNode) THEN
      NCERRORValue = XMLNode.InnerText;
    

    You will find all required variable types in the codeunit too (in function parameters definitions)

    Hi,
    I can't find this function in the c6224 LoadXMLDocumentFromText(XMLDoc,...). I also can't find the InnerText property on the XMLNode.

    Did I miss something? I am using 2009 R2

    Thanks!
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    edited 2018-05-21
    Hi,
    I tried to do this but my NCErrorValue gave me empty data value.

    Please help.

    //=====
    Variable:
    XmlDoc2 Automation 'Microsoft XML, v6.0'.DOMDocument60

    XmlDoc2.async := FALSE;
    XmlDoc2.load(XMLHTTP.responseBody);
    IF EXISTS(ReceiveFile) THEN ERASE(ReceiveFile);
    XmlDoc2.save(ReceiveFile);

    XmlDoc2.load(ReceiveFile);
    IF FindNode(XmlDoc2, '/ncresponse/@NCERROR', XmlNode) THEN
    NCErrorValue := XmlNode.text;
    ERROR('NCErrorValue = %1',NCErrorValue);

    //======
    FindNode()
    IF ISCLEAR(XMLRootNode) THEN
    EXIT(FALSE);

    IF NOT NormalCaseMode THEN
    NodePath := UPPERCASE(NodePath);
    FoundXMLNode := XMLRootNode.selectSingleNode(NodePath);

    IF ISCLEAR(FoundXMLNode) THEN
    EXIT(FALSE)
    ELSE
    EXIT(TRUE);

    Variables:
    ======
    XMLRootNode Automation 'Microsoft XML, 6.0'.IXMLDOMNode
    NodePath Text 250
    FoundXMLNode Automation'Microsoft XML,6.0'.IXMLDOMNode
    ======
  • Options
    Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-05-21
    Sorry, I did miss that this is the NAV/Navision Classic Client forum.

    The code looks pretty OK.

    Use some XPath testing website, like this one to make sure that your XML (uploaded from ReceiveFile) is valid, and that the FindNode returns something, although me thinks that it does otherwise you would be getting errors on accessing XmlNode.text on an uninitialized automation var

    Nb apart from debugging saving the XML into a file reloading it is not necessary.

    Try to examine other properties of the XmlNode var, maybe XmlNode.nodeValue, XmlNode nodeName to see what is returned:
    IF FindNode(XmlDoc2, '/ncresponse/@NCERROR', XmlNode) THEN BEGIN
      NodeValueTxt := XmlNode.nodeValue;
      nodeNameTxt := XmlNode.nodeName;
    END
    
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    Sorry, I did miss that this is the NAV/Navision Classic Client forum.


    Try to examine other properties of the XmlNode var, maybe XmlNode.nodeValue, XmlNode nodeName to see what is returned:
    IF FindNode(XmlDoc2, '/ncresponse/@NCERROR', XmlNode) THEN BEGIN
      NodeValueTxt := XmlNode.nodeValue;
      nodeNameTxt := XmlNode.nodeName;
    END
    

    I tried this, and still giving me an empty data on both
    NodeValueTxt := XmlNode.nodeValue;
    nodeNameTxt := XmlNode.nodeName;

    Any more suggestion?
  • Options
    Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Does the XPath validator website returns expected attribute name/value pair when you paste the XML and the /ncresponse/@NCERROR' XPath expression?

    If yes you may try to retrieve XmlNode.attributes list and then loop through all of them
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    edited 2018-05-22
    Does the XPath validator website returns expected attribute name/value pair when you paste the XML and the /ncresponse/@NCERROR' XPath expression?

    This is the XPATH Result
    Attribute='NCERROR=50001113'

    If yes you may try to retrieve XmlNode.attributes list and then loop through all of them
    Could you describe more detail on how to accomplish this?

    Thanks for helping!!!
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    edited 2018-05-22
    Can someone help me on this please?
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    Karenh wrote: »

    Thanks a lot KarenH, I'll try it out.
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    edited 2018-05-23
    ===
    CREATE(XMLDoc2);
    XMLDoc2.load(ReceiveFile);
    XMLNodeList := XMLDomDoc.selectNodes('ncresponse');
    XMLNodeCurr := XMLNodeList.nextNode();
    XMLAttributes := XMLNodeCurr.attributes;

    i := 0
    WHILE i < XMLAttributes.length DO BEGIN
    case XMLAttributes.item(i).nodeName of
    'NCERROR': NCErrorValue := XMLAttributes.item(i).text;
    end;
    i += 1;
    END;
    =====
    KarenH,
    I mimic the same logic from the link but I got an this error
    "You have specified an unknown variable
    length
    Define the variable under 'Global C/AL symbols."

    It seems the var XMLAttributes is using a wrong Automation data type?

    How can I fix this?

  • Options
    Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    edited 2018-05-23
    The "Attributes" property is a list of objects. Try using the same type for your XMLAttributes var as you have used for the XMLNodeList var.
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    edited 2018-05-23
    Hi Slawek, appreciate your response

    I changed the XMLAttributes to be the same with XMLNodeList var.

    I got this error message when calling the function
    w1natbkg8vu5.png

    Is there any other solution on solving this problem? Maybe load the .xml into a file and read it by using strpos until reach end of file?
  • Options
    Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    You have another examples in the link then @Karenh sent you. Have you tried them? Have you tried to google arond and read some MS XML documentation to see what you may need?
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    You have another examples in the link then @Karenh sent you. Have you tried them? Have you tried to google arond and read some MS XML documentation to see what you may need?

    Yes, I have Slawek and I am still unable to make it work.
  • Options
    Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Perhaps you need to install MS XML 6.0 on your machine.

    Export your object with your attempts to a text file and paste its contet here. If you don't want to paste the whole code include at least the bits you have tried (including the other version with and please include variables definition.

    And please do use code formatting feature, it makes pasted code much more readable
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • Options
    JLeePlnrJLeePlnr Member Posts: 26
    Perhaps you need to install MS XML 6.0 on your machine.

    Export your object with your attempts to a text file and paste its contet here. If you don't want to paste the whole code include at least the bits you have tried (including the other version with and please include variables definition.

    And please do use code formatting feature, it makes pasted code much more readable

    I am finally able to resolve the problem using the link from @Karenh.

    Thank you very much for your helps and response @Slawek_Guzek and @Karenh !!! I really appreciate it.

    You guys rock!!!!
Sign In or Register to comment.