JavaScript: parseXMLString() - a reminder to myself…
If you have a string containing a XML-document you need to tell the browser to parse the the DOM. Otherwise you can not access the nodes and work with it. Makes sense you say?!
yeah… but I’ve wasted far to much time on that.
So remember: If you have a XML in a string convert it to a DOM-object to work with it otherwise you will see very strange results.
Here is a sample function:
function parseXMLString(xmlString) { //for IE if (window.ActiveXObject) { var xml_tree = new ActiveXObject("Microsoft.XMLDOM"); xml_tree.async = "false"; xml_tree.loadXML(xmlString); } //for Mozilla, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { var parser = new DOMParser(); var xml_tree = parser.parseFromString(xmlString,"text/xml"); } return xml_tree; }
This function accepts a string as parameter and uses ActiveXObject(”Microsoft.XMLDOM”) [IE] or DOMParser() [the rest of the gang] to parse it.
