XMLHelper with Namespaces

raprohaska
Kilo Guru

ServiceNow Level: Beginner

Trying to parse a SOAP response and I have found all suggestions point to XMLDocument and XMLHelper but I see very little about what functions are exposed through these objects. I think my troubles are due to the namespace in the xml. Here is an example of the xml:

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

  <s:Header />

  <s:Body>
          <CreateIncidentResponse xmlns="http://tempuri.org/">

                  <CreateIncidentResult xmlns:a="http://schemas.datacontract.org/2004/07/Entities.CMS.ExternalServices"
                                                                              xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
         
                            <a:Messages />

                            <a:Success>true</a:Success>

                            <a:TicketID>I140000048</a:TicketID>

                    </CreateIncidentResult>

            </CreateIncidentResponse>

  </s:Body>

</s:Envelope>

var response = s.post();

var helper = new XMLHelper(response);

var obj = helper.toObject();

JSUtil.logObject(obj, 'obj Response: ');

Log Object: obj Response: Object @xmlns:s: string =

http://schemas.xmlsoap.org/soap/envelope/ s:Body: Object UpdateIncidentResponse:

Object @xmlns: string = http://tempuri.org/ UpdateIncidentResult: Object

@xmlns:a: string =

http://schemas.datacontract.org/2004/07/Entities.CMS.ExternalServices

a:Success: string = true a:TicketID: string = I140000048 a:Messages: null = null

@xmlns:i: string = http://www.w3.org/2001/XMLSchema-instance

I can see the values (success = true and TicketID = I140000048) in the object but I don't understand how to access those values from code. Since I can't step through the code it is difficult to understand the structure of the object that is created.

Sorry if I'm reposting a question but I haven't found a great example yet.

- AA

10 REPLIES 10

Solved: Thank you ServiceNow Support.



The solution was to replace line 8:


var innotassessionid = xmldoc.getNodeText("//ns:return");



with



var innotassessionid = xmldoc.getNodeText("//*[name()='ns:return']");



Reference on this solution approach can be found at:


Quick tip for ignoring namespaces with XPath - John J. Andersen



and



w2schools.com XPath, XQuery, and XSLT Functions