- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2019 01:10 AM
<?xml version="1.0" encoding="UTF-8" ?>
<test>
<one>
<two prova="ANNA">A</two>
<three>B</three>
</one>
<one>
<two prova="OSSO">C</two>
<three>D</three>
</one>
</test>
Hi community,
I need to retrieve the attributes from both nodes two: "ANNA" and "OSSO", how can I do this?
I am using XMLDocument2
Thanks you
Andrea
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2019 03:15 AM
No direct method like XMLDocument to fetch attributes. You can use this one which retrieves by index
var xmlString = "<test><one><two prova='ANNA'>A</two><three>B</three></one><one><two prova='OSSO'>C</two><three>D</three></one></test>";
var xmlDoc = new XMLDocument2(xmlString);
xmlDoc.parseXML( xmlString );
var node = xmlDoc.getNode("//test");
var childNodes = node.getChildNodeIterator();
while(childNodes.hasNext()){
var i = childNodes.next().toString();
var index = i.indexOf("prova");
if(index > -1){
var end = i.indexOf("\">", index);
gs.print(i.substring(index+7, end));
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2019 01:20 AM
Hi,
You can refer to below link to retrieve the node, node attributes, etc
https://docs.servicenow.com/bundle/london-application-development/page/script/server-scripting/concept/c_XMLDocumentScriptObject.html
Kindly mark my answer as correct if this solves your problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2019 01:53 AM
Hello,
This is easily possible using an XMLDocument class
var xmlString = "<test><one><two prova='ANNA'>A</two><three>B</three></one><one><two prova='OSSO'>C</two><three>D</three></one></test>";
var xmlDoc = new XMLDocument(xmlString);
var nodes = xmlDoc.getNodes("//test/one/two");
gs.print(nodes.item(0).getAttributes().item(0).getNodeValue());
gs.print(nodes.item(1).getAttributes().item(0).getNodeValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2019 02:57 AM
Yeah I know,
is not possible by using XMLDocument2?
Thanks
Andrea
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2019 03:15 AM
No direct method like XMLDocument to fetch attributes. You can use this one which retrieves by index
var xmlString = "<test><one><two prova='ANNA'>A</two><three>B</three></one><one><two prova='OSSO'>C</two><three>D</three></one></test>";
var xmlDoc = new XMLDocument2(xmlString);
xmlDoc.parseXML( xmlString );
var node = xmlDoc.getNode("//test");
var childNodes = node.getChildNodeIterator();
while(childNodes.hasNext()){
var i = childNodes.next().toString();
var index = i.indexOf("prova");
if(index > -1){
var end = i.indexOf("\">", index);
gs.print(i.substring(index+7, end));
}
}