JS获取XML内容!

XML
<?xml version="1.0" encoding="GB2312"?>
<myfile>
    
<title>tttt</title>
    
<author>ajie</author>
    
<email>ajie@aolhoo.com</email>
    
<date>20010115</date>
</myfile>
JS:
<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;
}
posted on 2004-09-08 09:31  edobnet  阅读(13316)  评论(6编辑  收藏  举报