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.

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

Dotychczas
Mega Guru

I tried it like that:

  var xmlUtils = new XMLUtilJS();
gs.info(xmlUtils.escapeForXMLText("test&test"));

but it returns undefined

kristian dimitr
Kilo Sage

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, '&lt;'  );
    result     = result.replace( /\>/g, '&gt;'  );
    return result;
}

gs.info(escapeForXMLText('test&test'))

 

Regards!

Kristian