a cross-broswer solution for parse xml fragment

 1 function parseXml(xml) {
 2         var xmldom = null;
 3 
 4         if (typeof DOMParser != "undefined") {
 5             xmldom = (new DOMParser()).parseFromString(xml, "text/xml");
 6             var errors = xmldom.getElementByTagName("parsererror");
 7             if (errors.length) {
 8                 throw new Error("XML parsing error:" + errors[0].textContent);
 9             }
10         } else if (document.implementation.hasFeature("LS", "3.0")) {
11             var implementation = document.implementation;
12             var parser = implementation.createLSParser(implementation.MODE_SYNCHRONOUS, null);
13             var input = implementation.createLSInput();
14             input.stringDate = xml;
15             xmldom = parser.parse(input);
16         } else if (typeof ActiveXObject != "undefined") {
17             xmldom = createDocument();
18             xmldom = loadXML(xml);
19             if (xmldom.parseError != 0) {
20                 throw new Error("XML parsing error:" + xmldom.parseError.reason);
21             }
22         } else {
23         throw new Error("NO XML parser available.");
24     }
25     return xmldom;
26     }

 

posted on 2013-05-18 00:25  雷音  阅读(158)  评论(0编辑  收藏  举报

导航