Parsing XML Response

mdidrikson
Kilo Contributor

Hello,

I have a business rule that makes a call to an external web service and receives an XML response. The response document returned is quite large (see attached document). I am having some issues parsing this XML document. Here is the code that I am using to parse the response document:



var response = request.getResponseDoc();
gs.log("Get Tickets response from Savvis: " + response);

gs.log("Parse the response");

//PARSE THE RESPONSE
try {
var savvisXML = new XMLDocument(response);
} catch (e) {
gs.log("Error parsing XML response from Savvis: " + e.toString());
}

I am receiving the following error:

Error parsing XML response from Savvis: java.lang.NullPointerException

My guess is the issue is with the

<![CDATA[
tags in the response document. If I make this call and these fields are not included, I am able to parse the document.

Couple of questions I have:

1. Is there a way to parse an XML document that contains the

<![CDATA[
tags?
2. Is there a good way to loop through a large XML document such as this one? I would like to grab the "key" and "value" elements from this document.

Thanks for your help!

Mark Didrikson

18 REPLIES 18

Mark,

I think your problem is that '/[@id=#id9]' is not a valid XPATH.

I have played around with it and believe the following script should get you down the road a ways...



gs.log("XML: " + xmlString);

var savvisXML = new XMLDocument(xmlString, true); // XML document is name
// space aware

var numberOfTickets = savvisXML
.getNodes("//getTicketsReturn/getTicketsReturn[@href]");
gs.log("Number of Tickets: " + numberOfTickets.toString());
var lenNumberOfTickets = numberOfTickets.getLength();
gs.log("Number of tickets from Savvis: " + lenNumberOfTickets);

for ( var i = 0; i < lenNumberOfTickets; i++) {

var currentTicketRef = numberOfTickets.item(i).getAttribute("href");
gs.log("Current Ticket Ref: " + currentTicketRef.toString());
var xpath = "/@id=" + currentTicketRef + "";
var strippedCurrentTicketRef = currentTicketRef.replace("#","");
gs.log("Stripped Current Ticket Ref: " + strippedCurrentTicketRef);
var xpath = "//*/multiRef[@id=\"" + strippedCurrentTicketRef + "\"]";
gs.log("XPATH: " + xpath);
var multiRef = savvisXML.getNode(xpath);
gs.log("Current Ticket href: " + numberOfTickets.item(i).getNodeName());
gs.log("Current Ticket node: "
+ numberOfTickets.item(i).getAttribute("href"));
gs.log("Current Ticket multiref: " + multiRef.toString());
gs.log("Multiref Node name: " + multiRef.getNodeName());
gs.log("Multiref Node Attribs: " + multiRef.getAttributes());
gs.log("Multiref attrib length: " + multiRef.getAttributes().getLength());

}


Thanks John! That does help me make some progress.

Thanks again.

Mark Didrikson


John,

I'm now trying to get the next set of nodes under the multiref node.

I am trying this XPATH expression: //*/multiRef[@id="id0"]//attributes/attributes, which I tested in a XPATH validation tool and it seems to work, but when I run the code in Service-Now, it gives me this error:



java.lang.RuntimeException: org.mozilla.javascript.PropertyException: Constructor for "TypeError" not found.
Caused by error in <refname> at line 127

124: var multiRefAttributesXpath = "//*/multiRef[@id=\"" + strippedCurrentTicketRef + "\"]//attributes/attributes";
125: gs.log("multiRefAttributesXpath: " + multiRefAttributesXpath);
126: var multiRefAttributesList = savvisXML.getNodes(multiRefAttributesXpath);
==> 127: gs.log("multiRefAttributesXpath: " + multiRefAttributesList.toString);
128: gs.log("Number of attributes on Multiref: " + multiRefAttributesList.getLength());
129:
130: }


I've played around with it in an XPATH validator and it seems to be a valid string.

Here is my code:



var multiRefAttributesXpath = "//*/multiRef[@id=\"" + strippedCurrentTicketRef + "\"]//attributes/attributes";
gs.log("multiRefAttributesXpath: " + multiRefAttributesXpath);
var multiRefAttributesList = savvisXML.getNodes(multiRefAttributesXpath);
gs.log("multiRefAttributesXpath: " + multiRefAttributesList.toString);
gs.log("Number of attributes on Multiref: " + multiRefAttributesList.getLength());


Thanks.

Mark


your "toString" should have parenthesis after it: "toString()". Try that and see if it gets you back in business.


Thanks John! That did the trick for me. I was figuring it was something simple 🙂