Created
December 13, 2010 11:48
-
-
Save teramako/738927 to your computer and use it in GitHub Desktop.
E4X to DOM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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