JS Read XML document

Source: Internet
Author: User
Tags xpath


<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 = 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 onload= "Getdatanext ()" >
<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= "<<" onclick= "Getdataprev ();" > <input type= "button" value= ">>" onclick= "Getdatanext ();" >
</form>
</body>

Run
This page will pass in and run the initialization of the script. You must make sure that the Order.xml document is on the same path as the jsxml.html.
The initialization section shows a new ActiveX object as an Msxml2.domdocument.3.0 object type, and then the script passes in the Order.xml document into memory and selects all the/order/item nodes. We use the/order/item node to identify the options that the document already contains.
The <body> standard in the document has an OnLoad attribute that enables the Web page to invoke Getdatanext () and initialize it. This feature can be used to get the next value from the XML document and display it in the form. We use a simple index to access specific options.
The forward (>>) and Backward (<<) buttons all use the same mechanism. Call Getdatanext () or Getdataprev () first in response to the OnClick event, which uses a logical method to avoid accessing our records outside of the document.
Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.