首页 > 代码库 > 使用JavaScript访问XML数据
使用JavaScript访问XML数据
在本篇文章中,我们将讲述如何在IE中使用ActiveX功能来访问并解析XML文档,由此允许网络冲浪者操纵它们。
这一网页将传入并运行脚本的初始化。你一定确保order.xml文档与jsxml.html在相同的相同的路径上。
初始化部分将一个新的ActiveX对象例示为MSXML2.DOMDocument.3.0对象类型,然后脚本传入order.xml文档到内存中,并选择所有的/Order/Item节点。我们使用/Order/Item节点以识别文档已经包含的选项。
文档中的标准有一个onLoad属性,这一属性能够使得网页调用getDataNext()而初始化。这一功能可用于从XML文档中获得下一个值并显示在表单中。我们使用一个简单的索引来访问特定的选项。
向前(>>)和向后(<<)按钮都使用相同的机制。首先响应onClick事件而调用getDataNext() 或者getDataPrev(),这两个函数使用了逻辑方法以避免文档以外的范围访问我们的记录。
1. [代码]order.xml
<?xml version="1.0" ?>
<Order>
<Account>9900234</Account>
<Item id="1">
<SKU>1234</SKU>
<PricePer>5.95</PricePer>
<Quantity>100</Quantity>
<Subtotal>595.00</Subtotal>
<Description>Super Widget Clamp</Description>
</Item>
<Item id="2">
<SKU>6234</SKU>
<PricePer>22.00</PricePer>
<Quantity>10</Quantity>
<Subtotal>220.00</Subtotal>
<Description>Mighty Foobar Flange</Description>
</Item>
<Item id="3">
<SKU>9982</SKU>
<PricePer>2.50</PricePer>
<Quantity>1000</Quantity>
<Subtotal>2500.00</Subtotal>
<Description>Deluxe Doohickie</Description>
</Item>
<Item id="4">
<SKU>3256</SKU>
<PricePer>389.00</PricePer>
<Quantity>1</Quantity>
<Subtotal>389.00</Subtotal>
<Description>Muckalucket Bucket</Description>
</Item>
<NumberItems>1111</NumberItems>
<Total>3704.00</Total>
<OrderDate>07/07/2002</OrderDate>
<OrderNumber>8876</OrderNumber>
</Order>
2. [代码]order.html
<form>
<table border="0">
<tr><td>SKU</td><td><input type="text" name="SKU"></td></tr>
<tr><td>Price</td><td><input type="text" name="Price"></td></tr>
<tr><td>Quantity</td><td><input type="text" name="Quantity"></td></tr>
<tr><td>Total</td><td><input type="text" name="Total"></td></tr>
<tr><td>Description</td><td><input type="text"
name="Description"></td></tr>
</table>css3教程
<input type="button" value="http://www.mamicode.com/ << " onClick="getDataPrev();">
<input type="button" value="http://www.mamicode.com/ >> " onClick="getDataNext();">
</form>
3. [代码]jsxml.html
<html>
<head>
<script language="JavaScript">
<!--
vari = -1;
varorderDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
orderDoc.load("order.xml");
var items = orderDoc.selectNodes("/Order/Item");
function getNode(doc, xpath) {
varretval = "";
var value = http://www.mamicode.com/doc.selectSingleNode(xpath);
if (value) retval = value.text;
return retval;
}
function getDataNext() {
i++;
if (i > items.length - 1) i = 0;
document.forms[0].SKU.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/SKU");
document.forms[0].Price.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/PricePer");
document.forms[0].Quantity.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/Quantity");
document.forms[0].Total.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/Subtotal");
document.forms[0].Description.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/Description");
}
function getDataPrev() {
i--;
if (i < 0) i = items.length - 1;
document.forms[0].SKU.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/SKU");
document.forms[0].Price.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/PricePer");
document.forms[0].Quantity.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/Quantity");
document.forms[0].Total.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item["+ i + "]/Subtotal");
document.forms[0].Description.value = http://www.mamicode.com/getNode(orderDoc,"/Order/Item[" + i + "]/Description");
}
// -->
</script>
</head>
<body onl oad="getDataNext()">
<h2>XML Order Database</h2>
<form>
<table border="0">
<tr><td>SKU</td><td><input type="text" name="SKU"></td></tr>
<tr><td>Price</td><td><input type="text" name="Price"></td></tr>
<tr><td>Quantity</td><td><input type="text"
name="Quantity"></td></tr>
<tr><td>Total</td><td><input type="text" name="Total"></td></tr>
<tr><td>Description</td><td><input type="text"
name="Description"></td></tr>
</table>http://www.huiyi8.com/css3/?
<input type="button" value="http://www.mamicode.com/ << " onClick="getDataPrev();"> <input type="button" value="http://www.mamicode.com/ >> " onClick="getDataNext();">
? </form>
</body>
</html>