Last active
November 30, 2016 07:11
-
-
Save myfreeer/ad95050ab5fc22c466fcc65c6e95444e to your computer and use it in GitHub Desktop.
parse Xml with invalid characters
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 parseXmlSafe = function parseXmlSafe(text) { | |
"use strict"; | |
text = text.replace(/(?:[\0-\x08\x0B\f\x0E-\x1F\uFFFE\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g, ""); | |
if (window.DOMParser) return new window.DOMParser().parseFromString(text, "text/xml");else if (ActiveXObject) { | |
var activeXObject = new ActiveXObject("Microsoft.XMLDOM"); | |
activeXObject.async = false; | |
activeXObject.loadXML(text); | |
return activeXObject; | |
} else throw new Error("parseXmlSafe: XML Parser Not Found."); | |
}; |
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
let parseXmlSafe = text => { | |
"use strict"; | |
text = text.replace(/[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/ug, ""); | |
if (window.DOMParser) return (new window.DOMParser()).parseFromString(text, "text/xml"); | |
else if (ActiveXObject) { | |
let activeXObject = new ActiveXObject("Microsoft.XMLDOM"); | |
activeXObject.async = false; | |
activeXObject.loadXML(text); | |
return activeXObject; | |
} else throw new Error("parseXmlSafe: XML Parser Not Found."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
consider using https://github.com/jindw/xmldom if you got the
XML Parser Not Found
error