宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应
您当前的位置: > 个人博客 > javascript

[JS]用js解析和加载xml文件实例源代码

2016-10-13 00:16:48 点击量:55 标签: 收藏本文

[JS]用js解析和加载xml文件实例源代码

  1. <html> 
  2. <head> 
  3. <script type="text/javascript"> 
  4. function parseXML() 
  5. try //Internet Explorer 
  6.   { 
  7.   xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
  8.   } 
  9. catch(e) 
  10.   { 
  11.   try //Firefox, Mozilla, Opera, etc. 
  12.     { 
  13.     xmlDoc=document.implementation.createDocument("","",null); 
  14.     } 
  15.   catch(e) 
  16.     { 
  17.     alert(e.message); 
  18.     return; 
  19.     } 
  20.   } 
  21. xmlDoc.async=false
  22. xmlDoc.load("/example/xmle/note.xml"); 
  23. document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue; 
  24. document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue; 
  25. document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue; 
  26. </script> 
  27. </head> 
  28.  
  29. <body onload="parseXML()"> 
  30. <h1>W3Schools Internal Note</h1> 
  31. <p><b>To:</b> <span id="to"></span><br /> 
  32. <b>From:</b> <span id="from"></span><br /> 
  33. <b>Message:</b> <span id="message"></span> 
  34. </p> 
  35. </body> 
  36. </html> 

XML文件

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>  
  2. <note> 
  3.   <to>George</to>  
  4.   <from>John</from>  
  5.   <heading>Reminder</heading>  
  6.   <body>Don't forget the meeting!</body>  
  7.   </note> 

 

ABOUT US
宜昌农人网络科技有限公司

我们是一家提供综合软件外包与弱电工程服务的技术公司。

业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。

公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室