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
Tera Guru

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