Home > Web Front-end > JS Tutorial > JS parsing XML example analysis

JS parsing XML example analysis

PHPz
Release: 2018-09-29 15:52:00
Original
2002 people have browsed it

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&#39;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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template