JavaScript解析 XML DOM
XML 转换为 JavaScript 可存取的对象
new ActiveXObject("Microsoft.XMLDOM"); 和 new DOMParser();
function convertXmlStringToNode2(sXml) {
try //Internet Explorer
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(sXml);
} catch (e) {
try //Firefox, Mozilla, Opera, etc.
parser = new DOMParser();
xmlDoc = parser.parseFromString(sXml, "text/xml");
} catch (e) {
alert(e.message)
return xmlDoc.documentElement
// 定义 xml
let strXml = "<Root><DataRow id='1' name='daFei' /></Root>";
let oRoot = convertXmlStringToNode2(strXml);
console.log(oRoot);
console.log(oRoot.childNodes);
for (let i = 0; i < oRoot.childNodes.length; i++) {
let oNode = oRoot.childNodes.item(i);
console.log(oNode.getAttribute("id"),oNode.getAttribute("name"));