- 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 03:23 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 03:29 AM
Hi
okay, I see the problem. In JavaScript (and in most of the other programming languages), it is not allowed to use line breaks within a value assignment.
In your case, the following code should work:
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>';
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 03:37 AM
If anyhow I am able to add backslash at the end of every line then also it will execute and complete my work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 03:54 AM
Hi
I think it's time to provide ALL details and explain exactly what you want to achieve. Playing this kind of question answer ping pong is no fun!
Kind regards
Maik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-21-2022 04:05 AM
Hi
I am integrating a third party tool using REST Action, where the payload is generated as application/xml format.
After posting the payload to third party I am getting the response which is getting stored in a string variable.
Consider var payloadX = response.getBody();
This payloadX variable contains the above XML in string format as it is. Now I want to parse it and store Change Number.
Two things I have tried:
1. Converting this payload to JSON using xmlToJSON() method and tried to parse the ticket
2. Converting this payload to Javascript object using XMLHelper().toObject(payloadX) API and tried to parse the ticket
In both of the case I got the error "Unterminated Literal String"
So I tried to print this payloadX in background script, and found that the string does not terminated properly (coming from the integrated tool)
I tried to insert backslash at the end of each line > does not work
I tried to insert ' ' + in for the each line > does not work
I tried to replace string with payloadX.replace("/r/n", "///r/n"); > does not work
Now I am not able to find a way to fetch ticket from this string as it is not getting parsed using JSON, XML or with String Operations.
I hope this clarifies my query.
Thanks,
Aks