JS获取XML内容
- 作者:网页模板
- 类型:文章教程
- 点击次数:
- 发布时间:2014-07-19 16:11
1、名称为myfile.xml的xml文件:
XmlDocument这样就可以通过XPATH来选择XML里的内容了:
<?xml version="1.0" encoding="utf-8" ?> <myfile> <title>tttt</title> <author>ajie</author> <email>ajie@aolhoo.com</email> <date>20010115</date> </myfile>2、html代码:
<html> <head> <script language="JavaScript" for="window" event="onload"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.load("myfile.xml"); root = xmlDoc.documentElement; //nodes = xmlDoc.documentElement.childNodes; document.all("title").innerText = root.childNodes.item(0).text; document.all("author").innerText = root.childNodes.item(1).text; document.all("email").innerText = root.childNodes.item(2).text; document.all("date").innerText = root.childNodes.item(3).text; </script> <title>在HTML中调用XML数据</title> </head> <body bgcolor="#FFFFFF"> <b>标题: </b> <span id="title"> </span><br> <b>作者: </b> <span id="author"></span><br> <b>信箱: </b> <span id="email"></span><br> <b>日期:</b> <span id="date"></span><br> </body> </html>这里XMLDoc相当于.NET里的,
XmlDocument这样就可以通过XPATH来选择XML里的内容了:
items = xmldoc.selectNodes("invoices/invoice"); items = xmldoc.selectNodes("*/invoice"); items = xmldoc.documentElement.selectNodes("invoice");
function invoiceTotal(invoice) { invoice = invoice.nextNode() items = invoice.selectNodes("items/item"); var sum = 0; for (var item = items.nextNode(); item; item = items.nextNode()) { var price = item.selectSingleNode("price").nodeTypedValue; var qty = item.selectSingleNode("qty").nodeTypedValue; sum += price * qty; } return sum; }(责任编辑:网页模板)
- 本文标签:
- JS获取XML内容