The CreatorCon Call for Content is officially open! Get started here.

Retrieve nodes with same name in a XML file

andreazaffaroni
Tera Expert

<?xml version="1.0" encoding="UTF-8" ?>
<test>
         <one>
                   <two>A</two>
                  <three>B</three>
        </one>
        <one>
                  <two>C</two>
                  <three>D</three>
         </one>
</test>

Hi Community,

Can you please help me to retrieve data "A" and "C" from the correspondent childNodes "two" when the parentNode has the same name. See the example above.

Thanks,

Andrea

 

1 ACCEPTED SOLUTION

Please try this out, I have tested

var xmlString = "<test><one><two>A</two><three>B</three></one><one><two>C</two><three>D</three></one></test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML( xmlString ); 
node = xmlDoc.getFirstNode( '//test/one/two' );
j=1;
while( node != null ) {
      var value = xmlDoc.getNodeText( '//test/one[' + j + ']/two');
       gs.info( 'Value: ' + value);
       j++;
       node = xmlDoc.getNextNode( node );
}

View solution in original post

8 REPLIES 8

Please try this out, I have tested

var xmlString = "<test><one><two>A</two><three>B</three></one><one><two>C</two><three>D</three></one></test>";
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML( xmlString ); 
node = xmlDoc.getFirstNode( '//test/one/two' );
j=1;
while( node != null ) {
      var value = xmlDoc.getNodeText( '//test/one[' + j + ']/two');
       gs.info( 'Value: ' + value);
       j++;
       node = xmlDoc.getNextNode( node );
}

Hi

your code is correct, i don't manage to reproduce it with my xml which is actually more complicated than the example I provided above.

Thanks.

Andrea

Hi,

do you know how to retrieve attributes using xmldocument2?

Andrea

Harsh Vardhan
Giga Patron

you can try this kind of sample test on "www.w3schools.com", they have explained very well

 

w3schools

 

adding one link here, it has one example to get the child node value.

 

Example : https://www.w3schools.com/xml/tryit.asp?filename=try_dom_nodelist