Using XPATH to get multiple values from the same node name

truzicki
Kilo Expert

Good morning.   I'm successfully using XPATH to grab a single value from a node and place it into a scratchpad variable.   However, when I try the same approach to getting multiple values, I'm getting an undefined value returned.   I've read through the wiki, John Andersen's blog, and even tutorials online about XPATH, but I'm still stumped.   Here's an example of what I'm trying to do:

 

Using the following example XML,

 

<soap:Envelope >

    <soap:Body>

          <BunchOfWordsResponse >

                <BunchOfWordsResult>

                      <string>dog</string>

                      <string>cat</string>

                      <string>bird</string>

                      <string>fish</string>

                      <string>jupiter</string>

                      <string>putty</string>

                      <string>phone</string>

                </BunchOfWordsResult>

          </BunchOfWordsResponse>

    </soap:Body>

</soap:Envelope>

 

I'd like to get a string that contains all the words -- dog, cat, bird, etc.   If I use something like [var myExampleString = responseXML.getNodeText('//string');], it comes back as undefined.   Can anyone offer any suggestions?

 

Thank you!

-Tim Ruzicki.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Tim,



Not sure whether you are still facing same or similar type of issue for parsing and fetching values for xml tags which are repeated under a parent tag.


Here is the script which returns all the values under <string> tag. You can then store them into an array and do whatever processing you need to.



var xmlString = the xml string , store it in this variable


var k=0;


var arr=[];


var xmldoc = new XMLDocument(xmlString);


var noParamNode = xmldoc.getNodes("//BunchOfWordsResponse/BunchOfWordsResult/*").getLength();


for(var k=1; k<noParamNode+1; k++){


var value = xmldoc.getNodeText("//string["+k+"]");


gs.print('Individual value for string tag is::'+value);


arr.push(value.toString());


}


gs.print('Array is::'+arr);


}



As seen in the screenshot below, you can I have printed the individual values for string tag and an array as well containing these values.



xml-parsing.JPG



Mark my reply as Correct and also hit Like and Helpful if you find my response worthy.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Steve L_
Tera Contributor

Hi, I am having the same issue and am testing some XPath scenarios. I'll keep you posted on my results.


Steve L_
Tera Contributor

Have you tried this link below:



XPath Examples


richam
Tera Expert

Hi Tim,


I know this is old post, were you able to get the desired output?


Thanks,


Richa


Hi Richa!   I can't remember if I got my answer back then, or if/how I modified my solution.   Long time ago!   But, I like Ankur's response to the question, so I marked his answer as correct.   Hopefully it'll help other folks searching for the same answer.



Very best regards,
-Tim.