Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

XML getNodeText not working

juliana3
Tera Contributor

I am trying to get the value of a node from a XML response, but its giving me the value null instead.

Here is the response:

XMLDOC: <?xml version="1.0" encoding="UTF-8"?><Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body><ServiceIncident xmlns="http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2"><RequesterID>IN28617860</RequesterID><ProviderID>INC0023912</ProviderID><Reporter><ReporterID/></Reporter><Transaction><Acknowledge>1</Acknowledge><StatusCode>0</StatusCode><Comment>Success - No Errors Encountered</Comment><TransactionName/><TransactionType>2</TransactionType><TransactionDateTime>2017-07-20T15:53:38.0z</TransactionDateTime><TransactionNumber>1500576818383</TransactionNumber><TransactionRouting>SYNC::TIGSNOINSR</TransactionRouting></Transaction></ServiceIncident></Body></Envelope>

Here is my Code xmldoc.getNodeText("//RequesterID")

And is returning null

1 ACCEPTED SOLUTION

henry_cheng
ServiceNow Employee
ServiceNow Employee

Hi Juliana,



Can you try below code? I verified it is working. You need to remove the starting "XMLDOC:" string as it is not part of the XML string.


========================


var xmlString = "<?xml version='1.0' encoding='UTF-8'?><Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'><Body>" +


"<ServiceIncident xmlns='http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2'><RequesterID>IN28617860</RequesterID><ProviderID>INC0023912</ProviderID>" +


"<Reporter><ReporterID/></Reporter><Transaction><Acknowledge>1</Acknowledge><StatusCode>0</StatusCode><Comment>Success - No Errors Encountered</Comment>" +


"<TransactionName/><TransactionType>2</TransactionType><TransactionDateTime>2017-07-20T15:53:38.0z</TransactionDateTime>" +


"<TransactionNumber>1500576818383</TransactionNumber><TransactionRouting>SYNC::TIGSNOINSR</TransactionRouting></Transaction></ServiceIncident></Body></Envelope>";




var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(xmlString);


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


========================



Cheers


Henry


View solution in original post

5 REPLIES 5

henry_cheng
ServiceNow Employee
ServiceNow Employee

Hi Juliana,



Can you try below code? I verified it is working. You need to remove the starting "XMLDOC:" string as it is not part of the XML string.


========================


var xmlString = "<?xml version='1.0' encoding='UTF-8'?><Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'><Body>" +


"<ServiceIncident xmlns='http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2'><RequesterID>IN28617860</RequesterID><ProviderID>INC0023912</ProviderID>" +


"<Reporter><ReporterID/></Reporter><Transaction><Acknowledge>1</Acknowledge><StatusCode>0</StatusCode><Comment>Success - No Errors Encountered</Comment>" +


"<TransactionName/><TransactionType>2</TransactionType><TransactionDateTime>2017-07-20T15:53:38.0z</TransactionDateTime>" +


"<TransactionNumber>1500576818383</TransactionNumber><TransactionRouting>SYNC::TIGSNOINSR</TransactionRouting></Transaction></ServiceIncident></Body></Envelope>";




var xmlDoc = new XMLDocument2();


xmlDoc.parseXML(xmlString);


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


========================



Cheers


Henry


It did work fine with XMLDocument2, my question is why doesn't work for XMLDocument, for some reason, is it the encoding of the message?


sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Works for me:



var xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +


"<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">" +


"     <Body>" +


"           <ServiceIncident xmlns=\"http://b2b.ibm.com/schema/IS_B2B_CDM/R2_2\">" +


"                 <RequesterID>IN28617860</RequesterID>" +


"                 <ProviderID>INC0023912</ProviderID>" +


"                 <Reporter>" +


"                       <ReporterID />" +


"                 </Reporter>" +


"                 <Transaction>" +


"                       <Acknowledge>1</Acknowledge>" +


"                       <StatusCode>0</StatusCode>" +


"                       <Comment>Success - No Errors Encountered</Comment>" +


"                       <TransactionName />" +


"                       <TransactionType>2</TransactionType>" +


"                       <TransactionDateTime>2017-07-20T15:53:38.0z</TransactionDateTime>" +


"                       <TransactionNumber>1500576818383</TransactionNumber>" +


"                       <TransactionRouting>SYNC::TIGSNOINSR</TransactionRouting>" +


"                 </Transaction>" +


"           </ServiceIncident>" +


"     </Body>" +


"</Envelope>";



var xmldoc = new XMLDocument(xmlString);


var str = xmldoc.getNodeText("//RequesterID");


gs.print(str);



with output:



[0:00:00.005] Script completed in scope global: script



*** Script: IN28617860

Khozema Attar1
Tera Guru

Hi Juliana,



Try with the following code:



var responseBody = response.getBody();


var xm = new XMLDocument2();


xm.parseXML(responseBody);


var ticketId=xm.getNodeText("//RequesterID");



Thanks & Regards