其他分享
首页 > 其他分享> > 解析 XML DOM

解析 XML DOM

作者:互联网

解析 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"));
  }

 

标签:XML,sXml,childNodes,DOM,let,new,oRoot,解析,xmlDoc
来源: https://www.cnblogs.com/dafei4/p/15849504.html