Skip to content

Instantly share code, notes, and snippets.

@teramako
Created December 13, 2010 11:48
Show Gist options
  • Save teramako/738927 to your computer and use it in GitHub Desktop.
Save teramako/738927 to your computer and use it in GitHub Desktop.
E4X to DOM
var XHTML = new Namespace("xhtml", "http://www.w3.org/1999/xhtml");
function xmlToDom (xml, xmlns) {
XML.prettyPringing = false;
XML.ignoreWhitespace = true;
var doc = (new DOMParser).parseFromString(
<root xmlns={xmlns || XHTML}>{xml}</root>.toXMLString(),
"application/xml"
);
var imported = document.importNode(doc.documentElement, true);
var range = document.createRange();
range.selectNodeContents(imported);
var fragment = range.extractContents();
range.detach();
return fragment.childNodes.length > 1 ? fragment : fragment.firstChild;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment