Monday, April 10, 2017

AX 2012: Reading XML Nodes under specific tag through X++

XML to read




















X++ Code 

For above XML structure, I will read all nodes exist within each InstructorAPIModel node. The getElementsByTagName() method of XmlDocument class can be used which returns XmlNodeList which can then be iterated to get node's values.

  XmlDocument doc = new XmlDocument();
XmlNodeList         apiModelList;
XmlNodeListIterator iterator;
XmlElement          apiModel;
XmlElement          firstName;
XmlElement          lastName;

doc.loadXml(_xmlMsg);

apiModelList = doc.getElementsByTagName('InstructorApiModel');
iterator= new XmlNodeListIterator(apiModelList);

while (iterator.moreValues())
{
    apiModel = iterator.value();
        
    firstName = apiModel.getNamedElement('FirstName');
    if (firstName)
    {
        info(strFmt("First Name: %1", firstName.text()));
    }
        
    lastName = apiModel.getNamedElement('LastName');
    if (lastName)
    {
        info(strFmt("Last Name: %1", lastName.text()));
    }

    iterator.nextValue();
}

3 comments:

  1. Hi Faisal,

    I guess you forgot to tell us about your parameter/variable "_xmlMSG".
    what kind type of _xmlMSG exactly?

    thank you for your help.

    regard,
    Husna

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. and how to define the frist tag/code in x++.
    'arrayodinstructorAPIModel xmlns:bla..bla..bla ...'
    Thank you for your reply

    ReplyDelete

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...