Read XML in AX


static void testReadXml(Args _args)
{
    str                 sourceXMLFile, sNumber, sName;
    XmlDocument         xmlDocument;
    XmlNodeList         nodeList, childNodeList;
    XmlNode             node;
    XmlElement          element;
    XMLNodeListIterator xmlNodeListIterator;
    Counter             counter;
    int                 i, j;
    ;
    sourceXMLFile = @"<Message><School id='203'>
        <Student>
            <Number>001</Number>
            <Name>Stud_A</Name>
            <Relation>
                <Id>1</Id>
            </Relation>
        </Student>
        <Student>
            <Number>002</Number>
            <Name>Stud_B</Name>
            <Relation>
                <Id>2</Id>
            </Relation>
        </Student>
        <Student>
            <Number>003</Number>
            <Name>Stud_C</Name>
            <Relation>
                <Id>3</Id>
            </Relation>
        </Student>
    </School></Message>";

    xmlDocument = XmlDocument::newXml(sourceXMLFile);

    // single node
    node    = xmlDocument.selectSingleNode('Message//School');
    element = xmlDocument.selectSingleNode('Message//School');
    // attribute
    info(element.getAttribute('id'));

    // list iteration #1
    nodeList = xmlDocument.selectNodes('//School//Student');
    xmlNodeListIterator = new xmlNodeListIterator(nodeList);
    while(xmlNodeListIterator.moreValues())
    {
        counter++;
        node = xmlNodeListIterator.value();

        sNumber = node.selectSingleNode('Number').text();
        sName = node.selectSingleNode('Name').text();
       
        info(strFmt("Record %1: Number - %2, Name - %3",
                        counter,
                        sNumber,
                        sName));
        xmlNodeListIterator.nextValue();
    }
    
    // list iteration #2
    nodeList = xmlDocument.selectNodes('Message//School//Student');
    for (i = 0; i < nodeList.length(); i++)
    {
        childNodeList = nodeList.item(i).childNodes();
        
        for (j = 0; j < childNodeList.length() ; j++) 
        {
            node = childNodeList.item(j);

            if (node.baseName() == 'Relation')
            {
                info(node.innerXml());
                break;
            }
        }        
    }
}


 

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics AX.

Authors are allowed to set their own AdSense units.
Join us.

Blog Tags