Skip to content

Instantly share code, notes, and snippets.

@ggendre
Created April 6, 2011 15:16
Show Gist options
  • Save ggendre/905833 to your computer and use it in GitHub Desktop.
Save ggendre/905833 to your computer and use it in GitHub Desktop.
javascript : convert a string to XML DOM
//this function convert the string representation of some XML
//into a DOM object.
function StringToXMLDom(string){
var xmlDoc=null;
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(string,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(string);
}
return xmlDoc;
}
// EXEMPLE FILE
//Ce bout de code retourne l'attribut url du premier tag image trouvé.
var xmlDom=StringToXMLDom(xmlString);
if (xmlDom){
var tmp=xmlDom.getElementsByTagName("image")[0];
if (tmp!=undefined){
var src=tmp.getAttribute("url");
if (src) return src;
}
}
@nguyenphi0205
Copy link

how to use it,man ?

@edgarechm
Copy link

in a code outside windows... where does window come from?
window.DOMParser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment