The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Unterminated Literal String

aks4
Tera Contributor

I have a response stored in a string variable and want to fetch the ticket number but I am not able to traverse through the output.

Output : <?xml version="1.0" encoding="ISO-xxxx-xx" standalone="yes"?>
<Web-Ticket>
<Change>
<pro>
<text>Change successfully created</text>
<ticket>CHG00001</ticket>
</pro>
</Change>
</Web-Ticket>''

I want to extract the change number. I tried using xmlToJSON, I tried inserting '\' at the end of the line, I tried using XMLHelper.toObject API, but every time this is giving unterminated literal string error. Even if I store the output in a string field and print it, then the same error appears.

 

Any suggestion?

1 ACCEPTED SOLUTION

Hi @aks 

 

Please try the below-provided code in the background Script

Code:

var xmlStr = '';
xmlStr += '<?xml version="1.0" encoding="ISO-xxxx-xx" standalone="yes"?>';
xmlStr += '<Web-Ticket>';
xmlStr += '<Change>';
xmlStr += '<pro>';
xmlStr += '<text>Change successfully created</text>';
xmlStr += '<ticket>CHG00001</ticket>';
xmlStr += '</pro>';
xmlStr += '</Change>';
xmlStr += '</Web-Ticket>';

xmlStr = xmlStr.replace(/<\?xml.*\?>/g,'');

gs.info('*** \n' + xmlStr);


var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlStr);

gs.info(xmlDoc);
var node = xmlDoc.getNode("/Web-Ticket/Change/pro/ticket");
gs.info(node);

 

In your Scripted REST or Script Include you can modify the code as provided below:

Code:

var payloadX = response.getBody();

//Remove the XML declaration to allow XMLDocument2 to add its own Declaration
xmlStr = payloadX.replace(/<\?xml.*\?>/g,'');

gs.info('*** \n' + xmlStr);


var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlStr);

gs.info(xmlDoc);
var node = xmlDoc.getNode("/Web-Ticket/Change/pro/ticket");
gs.info(node);

This should work for you.

Please let me know if this helps!

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

View solution in original post

10 REPLIES 10

Maik Skoddow
Tera Patron
Tera Patron

Hi

in the last line there are quotation marks which makes the XML invalid

</Web-Ticket>''

Remove them and try again

Kind regards
Maik

 

@Maik Skoddow  Yes I tried that but the same error exists. 

 

Hi,

your XML is okay. Therefore please share your JavaScript code which causes the troubles.

Kind regards
Maik

var payloadX = '<?xml version="1.0" encoding="ISO-xxxx-xx" standalone="yes"?>
<Web-Ticket>
<Change>
<pro>
<text>Change successfully created</text>
<ticket>CHG00001</ticket>
</pro>
</Change>
</Web-Ticket>';

 

gs.info('Payload String'+payloadX);

 

Output Error: Unterminated lateral string line 1 : column 74