XML parsing of children nodes

erniec
Giga Expert

Good morning,

I am attempting to parse the childrenNodes of an xml response. I am able to parse the parent child record FINE but cant seem to parse and associate the children records to its parent.

This is an example of the xml response from the web service. I want to parse the Role as a child of the parent SecP object.

Using this line of code simply created records for each role. So I assume that I will need to iterate within a childNode but cannot find the code to do so.

var secRecordList=secDoc.getDocumentElement.getElementsByTagName("Role");

Anyone know how I can leverage this logic to parse the Role Children Objects.

Sorry for any typos and thanks in advance.

<SecP>

<SecName>John</SecName>

<startDate>2016-01-25</startDate>

<email>test@gmail.com</email>

        <Role>

                      <code>123</code>

          </Role>

<Role>

                      <code>456</code>

          </Role

</SecP>

<SecP>

<SecName>Jay</SecName>

<startDate>2016-01-24</startDate>

<email>test@gmail.com</email>

        <Role>

                      <code>789</code>

          </Role>

        <Role>

                      <code>111</code>

          </Role

</SecP>

var secDoc=New XMLDocument(response.getBody());

secDoc.parseXML(secDoc);

secDoc.getDocumentElement().normalize();

var secRecordList=secDoc.getDocumentElement.getElementsByTagName("SecP");

var secname;

var startDate;

var email;

for (var i=0;i<secRecordList.getLength();i++){

  var childList=secRecordList.item(i).getChildNodes();

       

        for (var j=1;j<childList.getLength();j++){

            var childNode=ChildList.item(j);

      if (childNode.getNodeName()=="SecName"){

secname=childNode.getTextContent();

}

  if (childNode.getNodeName()=="startDate"){

startDate=childNode.getTextContent();

}

  if (childNode.getNodeName()=="email"){

email=childNode.getTextContent();

}

}

}

1 ACCEPTED SOLUTION

amlesh
Kilo Sage

Hi Ernest,



Can you try running the below code in background script and let me know whether this resolves your request.




XML:



<SecP>


<SecName>John</SecName>


<startDate>2016-01-25</startDate>


<email>test@gmail.com</email>


      <Role>


<code>123</code>


          </Role>


<Role>


                      <code>456</code>


          </Role


</SecP>


<SecP>


<SecName>Jay</SecName>


<startDate>2016-01-24</startDate>


<email>test@gmail.com</email>


      <Role>


                      <code>789</code>


          </Role>


      <Role>


                      <code>111</code>


          </Role


</SecP>


     


Code:



var str = "<SecP><SecName>John</SecName><startDate>nowdate</startDate><email>testmail</email><Role><code>123</code></Role><Role><code>456</code></Role></SecP><SecP><SecName>Jay</SecName><startDate>now2</startDate><email>testmail2</email><Role><code>789</code></Role><Role><code>111</code></Role></SecP>";//use this when running the code in background script



var req=gs.xmlToJSON(request.body.dataString); //While running the script using 'background script' use the parameter as "Str" instead of " request.body.dataString "


var jsonString = JSON.stringify(req);


gs.print(jsonString);


for(var i=0;i<req.SecP.length;i++){


var SecName=req.SecP[i].SecName;


var startDate= req.SecP[i].startDate;


var email = req.SecP[i].email;


for(var j=0;j<req.SecP[i].Role.length;j++){


gs.print('code '+req.SecP[i].Role[j].code);


}


gs.print(SecName+','+startDate+','+email);


}


View solution in original post

5 REPLIES 5

Dave thanks for your response.


I am working on a standalone application so I had to manually type this out. Sorry for the typo.



What I am attempting to do is create a record for each parent and child in servicenow.   The children do not have foreign keys to the parent so I am having to parse the message at the node level. My question relates to taking a soap request and parsing the Children of the parent record.   I will try to go the xPath route because JSON will require me to strip out the namespace and header information of the request.



SecP1   PARENT



John     2016-01-25     test@mail.com



SecP1   CHILD



John       123


John     456