Monday, April 10, 2017

AX 2012: Reading XML Nodes through X++

XML to read



















X++ Code

XmlDocument     doc;
XmlNodeList     xmlScriptList;
XmlNodeList     xmlResponseList;
XmlNodeList     xmlTemplateList;
XmlElement      nodeScript;
XmlElement      nodeResponse;
XmlElement      nodeTemplate;
XMLParseError   xmlError;
str xmlMsg;
int i, j;
    ;
   
// Create the XML Document
doc = new XmlDocument();
doc.loadXml(xmlMsg);
// Verify XML Document Structure
xmlError  = doc.parseError();
if(xmlError && xmlError.errorCode() != 0)
{
    throw error(strFmt("XML Error: %1", xmlError.reason()));
}
// Get the root element and its child nodes
nodeScript = doc.getNamedElement("ns2:return");
xmlScriptList = nodeScript.childNodes();
for(i=0; i < xmlScriptList.length(); i++)
{
    nodeResponse = xmlScriptList.item(i);
    xmlResponseList = nodeResponse.childNodes();
    for (j=0; j < xmlResponseList.length(); j++)
    {
        nodeTemplate = xmlResponseList.item(j);
        xmlTemplateList = nodeTemplate.childNodes();
        if (nodeTemplate.selectSingleNode('id'))
        {
           info(strFmt("ID: %1", nodeTemplate.getNamedElement("id").text()));
        }
            
        if (nodeTemplate.selectSingleNode('messageTemplateName'))
        {
           info(strFmt("messageTemplateName: %1", nodeTemplate.getNamedElement("messageTemplateName").text()));
        }
    }
}

No comments:

Post a Comment

I will appreciate your comments !

How to enable new Microsoft teams - Public Preview!

New Microsoft Teams is just AWESOME, quick but useful post below shows how you have this preview feature to make your life EASY!  Open Micr...