Tuesday, January 20, 2009

Javascript converting XML Document to XML String

We can do it easily using XML Document object to load XML String to form an XML Document.
But, to do it the other way, there isn't a direct method  like .OuterXML in Javascript.

All along I've been using like this for my AJAX application

if (xmlDoc.xml!=undefined)   xmlContent = xmlDoc.xml; else   xmlContent = xmlDoc;

While it appears to be working when I send the xmlContent over the AJAX POST Call, recently there had been some error. It appears that webkit based browser like Safari and Chrome do not escape the special characters in the XMLTextNode while IE and Firefox do.

The solution is simple:

function XMLToString(oXML) {   if (window.ActiveXObject) {     return oXML.xml;   } else {     return (new XMLSerializer()).serializeToString(oXML);   } }


No comments: