- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2017 08:00 AM
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();
}
}
}
Solved! Go to Solution.
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2017 12:22 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2017 12:22 PM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2017 01:09 PM
Thanks Amlesh for your response. I will try this first thing tomorrow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2017 05:29 AM
Thank you Amlesh,
This is exactly what I was looking to accomplish. However there is one thing I did not take into account in the example. This is a soap response with header information in it. When I run the following I get this exception: misshaped element;
var response=s.execute();
var req=gs.xmlToJSON(response.body.dataString);
gs.print("This is the JSON String" + req);
Any suggestions.
Thanks a lot for your assistance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2017 01:36 PM
- <Role>
- <code>456</code>
- </Role
<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(); } } }
Just two points:
- your XML doesn't look well-formed, unless that's simply a typo.
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.
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(); } } }
Do you want the contents of //Role or are you after //Role/code ...?
Essentially you could use an XPath to return those nodes (which would be a listNode object, I believe) then parse it as though it was an array to fetch each sequential item. Is that what you're after?