reading nodes in xml
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2017 02:05 AM
hello,
i tried to read a value of a specific node ,
i want to read the content of all <two>
i tried this code but i have the error
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <one>" +
" <two att=\"xxx\">vvv </two>" +
" <three boo=\"yah\" att=\"yyy\">vvv</three>" +
" <two>vvv</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
var t=0;
var node2 = xmlDoc.getNode('//test');
var iter2= node2.getChildNodeIterator();
var node = xmlDoc. getFirstNode('//one');
while(iter2.hasNext()) {
var in2 = node.getChildNodeIterator();
var nodet = xmlDoc.getFirstNode('//two');
while (in2.hasNext()){
gs.print(nodet);
var nodetn = xmlDoc.getNextNode(nodet);
}
var test= xmlDoc.getNextNode(node);
}
i can show all the <two> but i got this error too
how can i handle it pls ?
thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2017 03:49 PM
//Below script prints all node 'two' values and also attributes
//PLEASE mark CORRECT if that solves your problem
var xmlString = "<test>" +
" <one>" +
" <two att=\"xxx\">abcd1234</two>" +
" <two att=\"xxx\">abcd1234</two>" +
" <three boo=\"yah\" att=\"yyy\">1234abcd</three>" +
" <two>another</two>" +
" </one>" +
" <one>" +
" <two att=\"xxx\">vvv </two>" +
" <three boo=\"yah\" att=\"yyy\">vvv</three>" +
" <two>vvv</two>" +
" </one>" +
" <number>1234</number>" +
"</test>";
var xmldoc = new XMLDocument(xmlString);
var nodelist = xmldoc.getNodes("//one/*");
gs.log("Total nodes under parent One "+nodelist.getLength());
gs.log('\n');
for (var x=0; x<nodelist.length;x++) {
if (JSUtil.notNil(nodelist.item(x).getNodeName()) && nodelist.item(x).getNodeName().toLowerCase() == 'two'){
//printing node value
gs.log('Node Two Value is -----'+nodelist.item(x).getLastChild().getNodeValue());
//printing node attribute
if (JSUtil.notNil(nodelist.item(x).getAttributes()) && JSUtil.notNil(nodelist.item(x).getAttributes().item(0))) {
gs.log('Corresponding Attribute Value is ---'+nodelist.item(x).getAttributes().item(0).getNodeValue());
}
}
}