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

habin
Kilo Contributor

I think , this can help : http://wiki.service-now.com/index.php?title=Parsing_XML_Payloads_Using_Business_Rules


mdidrikson
Kilo Contributor

Is it possible to use variables when parsing an XML document?

For example, would this work?



var currentTicketRef = numberOfTickets.item(i).getAttribute("href");
varMultiRef = savvisXML.getNode("/[@id=currentTicketRef]").getNodeValue();
gs.log("Current Ticket multiref: " + varMultiRef);


The value of currentTicketRef = "id0"

Thanks.

Mark Didrikson


Yes Mark, you can use variables in your parsing. If I understand you correctly, you would like the value of currentTicketRef to be used in the xpath query. Couldn't you just type:



varMultiRef = savvisXML.getNode("/[@id=" + currentTicketRef + "]").getNodeValue();


-John


Thanks. I tried what you suggested, but I got this error:

Extra illegal tokens: '[', '@', 'id', '=', '#id9', ']'org.apache.xpath.compiler.XPathParser.error(XPathParser.java:608)
org.apache.xpath.compiler.XPathParser.initXPath(XPathParser.java:143)

Here is what I'm trying to accomplish:

What I'm currently trying to do is retrieve the element in the document with a id="id0". I'll then want to locate other elements based on an attribute value.



<multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns4:PortalObject" xmlns:ns4="http://util.ws.data.ccc.ui.savvis.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">


Here is my code that I'm currently working on. I am performing a loop over a number of nodes:



var numberOfTickets = savvisXML.getNodes("//getTicketsReturn/getTicketsReturn[@href]");
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");
var multiRef = savvisXML.getNode("/[@id=" + currentTicketRef + "]");
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);

}


Thanks!

Mark Didrikson