JavaScript获取XML内容
XML文件内容:
1 <?xml version="1.0" encoding="gb2312"?>
2 <student>
3 <id>001</id>
4 <name>好大一棵草</name>
5 <email>好大一棵草@gmail.com</email>
6 <date>2011-05-19</date>
7 </student>
页面获取XML文件内容的JS脚本:
(经调试此js脚本在IE浏览器上有效,但在FF4上无效)
1 <script type="text/javascript" language="JavaScript" for="window" event="onload"> //for="window" event="onload"必须要,否则会出错
2 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
3 xmlDoc.async="false";
4 xmlDoc.load("introduce.xml");//introduce.xml为所获取xml文件
5 root = xmlDoc.documentElement;
6 //nodes = xmlDoc.documentElement.childNodes;
7 document.all("id").innerText = root.childNodes.item(0).text;
8 document.all("name").innerText = root.childNodes.item(1).text;
9 document.all("email").innerText = root.childNodes.item(2).text;
10 document.all("date").innerText = root.childNodes.item(3).text;
11 </script>
HTML页面代码:
1 <body>
2 <form id="form1" runat="server">
3 <div>
4 <b>编号: </b><span id="id"></span>
5 <br/>
6 <b>姓名: </b><span id="name"></span>
7 <br/>
8 <b>信箱: </b><span id="email"></span>
9 <br>
10 <b>日期:</b> <span id="date"></span>
11 <br/>
12 </div>
13 </form>
14 </body>