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

JS parsing XML example analysis

PHPz
PHPzOriginal
2016-05-16 16:16:451968browse

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>

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!

Statement:
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