Parsing XML and not able to get the node value I want

Jason Stephens
Kilo Guru

Hello Everyone!

I am trying to parse a value out of an XML response, and I can't seem to get it.   The response is returning correctly to me as needed (verifying using tostring() as you see below), but when I try to grab the "RequesterID" value that I need, I always get a return value of "null".   Code Snippet below as to how I'm trying to grab it, and I have also attached the response.     Pretty sure this is something simple, but I'm missing it...

var xmlDoc = new XMLDocument2();  

xmlDoc.parseXML(responseBody);

//gs.info(xmlDoc.toString());

gs.info(xmlDoc.getNodeText("//RequesterID"));

Thanks for any help!

Jason

10 REPLIES 10

venkatiyer1
Giga Guru

Hi Jason,



Have you tried gs.info(xmlDoc.getNodeText("//ServiceIncident/RequestedID"))


Hi Venkat,



Thank you for the reply. I just tried this and I still get "null".


Robert Beeman
Kilo Sage

I got it to return '1177' with your code in scripts - background. I had to delete this line first though because it kept throwing an error when I left it in:


<?xml version="1.0" encoding="utf-8"?>



Error:


  1. org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs.
  2. at [row,col {unknown-source}]: [1,194]

So this is what I ran so you can try to reproduce it (Note: I deleted that line I mentioned above from line #5):



var responseBody =   '   <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">   '   +


'           <s:Body>   '   +


'                   <TESTResponse xmlns="http://tempuri.org/">   '   +


'                           <TESTResult>   '   +



'                                   <ServiceIncident>   '   +


'                                           <ProviderID>Test Ticket</ProviderID>   '   +


'                                           <RequesterID>1177</RequesterID>   '   +


'                                           <Transaction>   '   +


'                                                   <Acknowledge>1</Acknowledge>   '   +


'                                                   <StatusCode>0</StatusCode>   '   +


'                                                   <Comment>No Errors Encountered</Comment>   '   +


'                                                   <TransactionName>Problem_Submittal</TransactionName>   '   +


'                                                   <TransactionType>2</TransactionType>   '   +


'                                                   <TransactionDateTime>4/13/2016 6:20:25 PM</TransactionDateTime>   '   +


'                                                   <TransactionNumber>1875869</TransactionNumber>   '   +


'                                           </Transaction>   '   +


'                                   </ServiceIncident>   '   +


'                           </TESTResult>   '   +


'                   </TESTResponse>   '   +


'           </s:Body>   '   +


'   </s:Envelope>   ' ;



var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(responseBody);



gs.print(xmlDoc.getNodeText("//RequesterID")); // 1177