Using JavaScript to access XML data

Source: Internet
Author: User
Tags xpath access
javascript|xml| Access | data

Java's access to XML files was previously done. I later listened to one of my colleagues (the technology is very good) said: JavaScript access is more convenient and simple. So today I went home to find the data to test the next. Very convenient indeed. Welcome everyone to share! I posted the test code I was looking for. It was copied from someone else. Or changed a few mistakes ha. There will be more information on this in the future ha! Thank you for your attention! The original code please click to read the full text.

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

<script language= "JavaScript" >
<!--
var i =-1;

var orderdoc = new ActiveXObject ("MSXML2. domdocument.3.0 ");
Orderdoc.load ("Order.xml");
var items = orderdoc.selectnodes ("/order/item");

function GetNode (Doc, XPath) {
var retval = "";
var value = 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 = GetNode (Orderdoc, "/order/item[" + i + "]/sku");
Document.forms[0]. Price.value = GetNode (Orderdoc, "/order/item[" + i + "]/priceper");
Document.forms[0]. Quantity.value = GetNode (Orderdoc, "/order/item[" + i + "]/quantity");
Document.forms[0]. Total.value = GetNode (Orderdoc, "/order/item[" + i + "]/subtotal");
Document.forms[0]. Description.value = GetNode (Orderdoc, "/order/item[" + i + "]/description");
}

function Getdataprev () {
i--;
if (i < 0) i = items.length-1;

Document.forms[0]. Sku.value = GetNode (Orderdoc, "/order/item[" + i + "]/sku");
Document.forms[0]. Price.value = GetNode (Orderdoc, "/order/item[" + i + "]/priceper");
Document.forms[0]. Quantity.value = GetNode (Orderdoc, "/order/item[" + i + "]/quantity");
Document.forms[0]. Total.value = GetNode (Orderdoc, "/order/item[" + i + "]/subtotal");
Document.forms[0]. Description.value = GetNode (Orderdoc, "/order/item[" + i + "]/description");
}

-->
</script>
<body >
<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>
<input type= "button" value= "<<" > <input type= "button" value= ">>" >
</form>
</body>


Put two files in the same directory



Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.