The CreatorCon Call for Content is officially open! Get started here.

XML String Parse Help Requested: XMLDocument2()

toneyvecchio1
Tera Expert

I was hoping someone might be able to help a newbie in SOAP/XML world.

I am successfully able to make outbound SOAP call and retrieve a response. I am trying to build the bones of code that will make the get call and create a record based on response fields but I am having a very difficult time trying to parse.

try {

  //   Initialize SOAP call

  var s = new sn_ws.SOAPMessageV2('x_rer_dell_sw_intg.Dell SecureWorks Ticketing', 'getUpdates');

  //   Set SOAP parameters

  s.setStringParameter('parameters'); //Appropriate parameters are set here

  //   Execute and store returned data.

  var response = s.execute();

  var responseBody = response.getBody();

  var status = response.getStatusCode();

  gs.info("Response\n\n" + response); //I see the object returned

  gs.info("ResponseBody\n\n" + responseBody);   //I see the expected XML returned

}

catch(ex) {

  var message = ex.getMessage();

  //gs.info(message); //Need to handle errors.

}

var xmlDoc = new XMLDocument2();

xmlDoc.parseXML(responseBody);

//xmlDoc.parseXML(response);

//var node = xmlDoc.getNode('//ticketId');

//gs.info(node.getNodeName());

//gs.info(node);

//var ticket = xmldoc.getNodeText("//ticketId");

//gs.info(xmlDoc.getNodeText("//ticketId"))

Its at line 23/24 where things breakdown, either one I try. I know because I have arbitrary insert incident gliderecord below code which doesnt run once those lines are up. I feel like I am following documentation correctly and have tried dozens of different ways. That said, I looked through error log and it directed me to an error in my SOAP response. I attached the full error message but the part i think is important is

fe

om.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'

at [row,col {unknown-source}]: [1,1]: org.apache.axiom.om.OMException: com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character '-' (code 45) in prolog; expected '<'

I then investigated the SOAP response and I think that does look strange, that it has --uuid: at top but I dont know if thats an issue. Is that like'y something that would cause an issue or am I chasing down the wrong rabbit hole? Do I need to convert it to a string, remove to a certain point and load it back as XML?

--uuid:283407f3-22bc-4fb1-8933-b331bb03e406

Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"

Content-Transfer-Encoding: binary

Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><env:Header xmlns:env="http://www.w3.org/2003/05/soap-envelope"/><soap:Body><ns2:getUpdatesResponse xmlns:ns2="http://service.ticket.api.mod.secureworks.com/"><ticket><client><id>x</id><name>x</name></client><contact><id>x</id><name>x</name></contact><dateCreated>x</dateCreated><dateModified>x</dateModified><detailedDescription>Text

Text
Text
</detailedDescription><devices><id>x</id><name>x</name></devices><eventSource>x</eventSource><isGlobalChild>x</isGlobalChild><isGlobalParent>x</isGlobalParent><location><id>x</id><name>x</name></location><reason>x</reason><responsibleParty>x</responsibleParty><service>x</service><severity>x</severity><status>x</status><symptomDescription>x</symptomDescription><ticketId>x</ticketId><ticketType>x</ticketType><ticketVersion>x</ticketVersion></ticket></ns2:getUpdatesResponse></soap:Body></soap:Envelope>

--uuid:283407f3-22bc-4fb1-8933-b331bb03e406--

I found online references to this in soap where users had to change app config but I see no way to that in Servicenow?

<binding name="BindingName" messageEncoding="Mtom" maxReceivedMessageSize="1006710886">

To

<binding name="BindingName" messageEncoding="Text" maxReceivedMessageSize="1006710886">

This is important for anyone running ksoap2 as it will not process the mtom message and will throw an xml parsing exception. - Stackoverflow forum

1 ACCEPTED SOLUTION

toneyvecchio1
Tera Expert

Should it help anyone in the future, its because the returned XML had UUID information outside of the SOAP envelope. After using string.replace and substring to remove from the top and bottom of the XML leaving only the <soap:Envelope> to </soap:Envelope> it all worked!


View solution in original post

1 REPLY 1

toneyvecchio1
Tera Expert

Should it help anyone in the future, its because the returned XML had UUID information outside of the SOAP envelope. After using string.replace and substring to remove from the top and bottom of the XML leaving only the <soap:Envelope> to </soap:Envelope> it all worked!