The Zurich release has arrived! Interested in new features and functionalities? Click here for more

reading nodes in xml

soukayna
Tera Contributor

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

find_real_file.png

how can i handle it pls ?

thanks

1 REPLY 1

Sashi K1
Kilo Guru

//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());      


          }


      }


}