Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How do I convert XML object to String ? Requirement is: to replace < and > with < and > respectively and make the string back to XML format. Sample XML is shown as below.

nm13
Kilo Guru
<res0:value>
	 &lt;ns4:id xmlns:ns4="urn:v3.diagnostic.dar.services.bgc"&gt;20182300020601349&lt;/ns4:id&gt;
	 &lt;ns4:globalResult xmlns:ns4="urn:v3.diagnostic.dar.services.bgc"&gt;Done&lt;/ns4:globalResult&gt;
	 &lt;ns4:detailedResultsBlock xmlns:ns4="urn:v3.diagnostic.dar.services.bgc"&gt;
		&lt;ns4:id&gt;20182300020601349&lt;/ns4:id&gt;
		&lt;ns4:result name="PABX" time="2018-06-08T17:24:36.954+02:00"&gt;No or private PABX.&lt;/ns4:result&gt;
		&lt;ns4:result name="Dare missing information" time="2018-06-08T17:24:36.954+02:00"&gt;Dare PTS potentially impacted by missing information from: ABR TSI, OSS network and services&lt;/ns4:result&gt;
		&lt;ns4:result name="Physical layer and connectivity"
					time="2018-06-08T17:24:36.954+02:00"&gt;The DSL line of the customer is in synch and quality is OK.&lt;/ns4:result&gt;
		&lt;ns4:result name="General information" time="2018-06-08T17:24:36.954+02:00"&gt;No general issues found.&lt;/ns4:result&gt;
	 &lt;/ns4:detailedResultsBlock&gt;
</res0:value>

Any help is much appreciated. Thank You!

1 ACCEPTED SOLUTION

nm13
Kilo Guru

I found a workaround for the issue. Here's the code. 

Suppose your XML is in a variable called responseBody

var xmlString = responseBody.toString();
var xmlClean = global.JSUtil.unescapeText(xmlString);

JSUtils is an OOB Script Include already present in every instance.

The xmlClean (String) is free from &lt; and &gt;

To convert it back to XML, use this code:

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

 

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron

Hi Nawshi,

So you want the following:

&lt; -> to be replaced with <

&gt; -> to be replaced with >

use below script and it will replace:

var data = ''; // your xml string

data = data.replaceAll("<","&lt;");

data = data.replaceAll(">","&gt;");

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

replace works only with String, the Xml in question is an XML Object. I found a workaround and I have mentioned in the answer to this question. Thanks anyway!

 

nm13
Kilo Guru

I found a workaround for the issue. Here's the code. 

Suppose your XML is in a variable called responseBody

var xmlString = responseBody.toString();
var xmlClean = global.JSUtil.unescapeText(xmlString);

JSUtils is an OOB Script Include already present in every instance.

The xmlClean (String) is free from &lt; and &gt;

To convert it back to XML, use this code:

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