Home > Article > Web Front-end > JS parsing XML example analysis
The example in this article describes the method of JS parsing XML. Share it with everyone for your reference. The specific implementation method is as follows:
<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>
The above is the entire content of this chapter. I hope it will be helpful to everyone’s JavaScript programming. For more related tutorials, please visit JavaScript Video Tutorial!