Escaping special characters in XML

Dotychczas
Mega Guru

Hello, i need to escape special characters from string to safely insert them into XML document (for example "&" to "&amp" etc). Is there any build in function to do that? 

11 REPLIES 11

kristian dimitr
Tera Guru

Hello,

You can try using this API (XMLUtilJS): 

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_XMLUtilJSAPI

Regards!

Kristian

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Are you using this in the outbound rest or soap message?

if yes then you can use this function to escape the XML characters

setStringParameter()

Regards
Ankur

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

Hi,

setStringParameter() -> to set simple string values such as sys_created_by which won't have special characters

XML reserved characters in the value are converted to the equivalent escaped characters.

setStringParameterNoEscape() -> to set work notes etc where end user can enter special character and in case you want to send that special character as it is without escaping

It does not escape XML reserved characters.

XML reserved characters are: 

> 
<  
&
%  

Regards
Ankur

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

I'm using it in flow designer. Getting it in script step and than populating xml message in rest step.