- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 12:53 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 04:11 AM
Hi
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 01:18 AM
Hi
in the last line there are quotation marks which makes the XML invalid
</Web-Ticket>''
Remove them and try again
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 01:24 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 02:14 AM
Hi,
your XML is okay. Therefore please share your JavaScript code which causes the troubles.
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 02:21 AM
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