首页 > 代码库 > JS解析XML实例

JS解析XML实例

<script  type="javascript">
var txt="<note>";
txt=txt+"<to>George</to>";
txt=txt+"<from>John</from>";
txt=txt+"<heading>Reminder</heading>";
txt=txt+"<body>Don't forget the meeting!</body>";
txt=txt+"</note>";

if (window.DOMParser)  //非IE浏览器
  {
   parser=new DOMParser();
   xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else //IE浏览器
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
   xmlDoc.async="false";
   xmlDoc.loadXML(txt);
  }

document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>


JS解析XML实例