Escaping special characters in XML
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2021 11:36 PM
Hello, i need to escape special characters from string to safely insert them into XML document (for example "&" to "&" etc). Is there any build in function to do that?
Labels:
- Labels:
-
Scripting and Coding
11 REPLIES 11
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2021 12:11 AM
I tried it like that:
var xmlUtils = new XMLUtilJS();
gs.info(xmlUtils.escapeForXMLText("test&test"));
but it returns undefined

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2021 01:23 AM
In this case, you can use a simple function like this which is the actual logic behind the API function:
function escapeForXMLText (str) {
var result = str.replace( /\&/g, '&' );
result = result.replace( /\</g, '<' );
result = result.replace( /\>/g, '>' );
return result;
}
gs.info(escapeForXMLText('test&test'))
Regards!
Kristian