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

So I'm a little confused now.   I caused the addition of the extra line because I used the "XML" button to view the response when I pasted it.   The actual response that I see in the log doesn't include that first line, so what you are suggesting should work.   Hmmm..



Jason


Thanks for the input Robert,



So I guess this means I need to do some cleaning up of the response before I parse it (or, if possible, get it sent without that line).



Thanks again,



Jason


The regex that is required in addition to what Robert said for removing the xml tags is added below for reference:



var str =   responseBody.replace(new RegExp('\\<\\?xml(.+?)\\?\\>', 'gi'), '');


var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(str );


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


Hi Venkat,



Thanks once again for your reply, but still no go.   My issue must be further up in the code, so I have posted an abbreviated version of the entire business rule (removed some variables and some setStringParameters to shorten) .   Do you see anything that's obviously wrong?   The 3 bold lines that are commented out are what I was trying to use before you offered the solution with the regex.



Thank you very much for the help!



try {  


var s = new sn_ws.SOAPMessageV2('XXXXX', 'XXXXXXXX');



//override authentication profile


//authentication type ='basic'


//r.setAuthentication(authentication type,profile name);



var incnumber = current.number;


var transnumber = current.sys_id;


var priority = current.priority;


var incstatus = current.state;



s.setStringParameter('vi.providerIDField', incnumber);


s.setStringParameter('vi.priorityField', priority);


setStringParameter('vi.statusField', incstatus);


s.setStringParameter('vi.transactionNumberField', transnumber);



var response = s.execute();


var responseBody = response.getBody();


var status = response.getStatusCode();


   


//var xmlDoc = new XMLDocument2();  


//xmlDoc.parseXML(responseBody);


//gs.info("XMLNode: " + xmlDoc.getNodeText("//RequesterID"));



var str =   responseBody.replace(new RegExp('\\<\\?xml(.+?)\\?\\>', 'gi'), '');


var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(str );


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



  }


catch(ex) {


var message = ex.getMessage();



}  


venkatiyer1
Giga Guru

Hi Jason,




I am assuming you have have created an outbound SOAP service call and you are referring to that when you are using the method SOAPMessageV2. Also assuming that you have defined MidServer in that message record itself as I didn't see that in the above code. If so, point to check would be your xml of SOAP.




The above code that you have pasted looks good to me and I dont see an issue in it. There are only two points of error either it is in SOAP message so we need to check the WSDL especially for the <soap-env:body> or else I would be checking for the ECC queue when the record comes in via mid server.