Skip to content

Instantly share code, notes, and snippets.

@myfreeer
Last active November 30, 2016 07:11
Show Gist options
  • Save myfreeer/ad95050ab5fc22c466fcc65c6e95444e to your computer and use it in GitHub Desktop.
Save myfreeer/ad95050ab5fc22c466fcc65c6e95444e to your computer and use it in GitHub Desktop.
parse Xml with invalid 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.");
};
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.");
}
@myfreeer
Copy link
Author

consider using https://github.com/jindw/xmldom if you got the XML Parser Not Found error

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