在html中解析xml文件(javascript 读取)

来源:互联网 发布:零基础学编程 先学什么 编辑:程序博客网 时间:2024/04/28 13:09
<?xml version="1.0"?><note><to>George</to><from>John</from><heading>Reminder</heading><body>Don't forget the meeting!</body><lastname>Simth</lastname></note>


以上是note.xml的内容。

下面note.html是解析note.xml的内容:

<html><head><script type="text/javascript">function parseXML(){try{xmlDoc= new ActiveXObject("Microsoft.XMLDOM");}catch(e){try{xmlDoc= document.implementation.createDocument("","",null);}catch(e){alert(e.message);return;}}xmlDoc.async = false;xmlDoc.load("../xml/note.xml");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></head><body onload="parseXML()"><h1>W3School.com.cn Internal Note</h1><p><b>To:</b><span id="to"></span><br/><b>From:</b><span id="from"></span><br/><b>Message:</b><span id="message"></span><p></body></html>