search
HomeBackend DevelopmentXML/RSS TutorialAccess XML data using JavaScript

Among web browser software, Internet Explorer (IE) is now a standard software. As you can see, almost every machine running a different version of the Windows operating system (and many other operating systems) uses IE. Microsoft has included the functionality of IE in implementing mature xml processing technology through ActiveX controls.

In this article, we will describe how to use ActiveX features in IE to access and parse XML documents, thereby allowing web surfers to manipulate them.

Web surfing
We start with a standard sequential document, as shown in Table A. This document contains simple sequential data for browsing by web surfers. Not only to display this data, we also provide a simple user interface that anyone surfing the Internet can use to browse XML documents.

Table A: 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>

We use a web form to access this XML document. This form will display the SKU, price, quantity, and subtotal of each part. , and a description of each option in the sequence. Our form also contains buttons for forward and backward browsing options.

Composition of a web page
The important part of a web page is the form. We will use a table to display it on the screen in an easy-to-read manner. The following is a code snippet for displaying an HTML table:

<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>

Please note that we include two buttons below the table, which are to browse the previous and next records through the getDataNext() and getDataPrev() functions. , this is also the issue we want to discuss.

Script
In fact, the essential part of our web page is not the form, but the script that controls the form. Included in our script are four parts. First, we initialize the web page by loading an XML document. The second part is to navigate to the next record. The third step is to navigate to the previous record. The fourth part is to extract a single value from the XML document. Table B shows the entire content of our web page.

Table B: 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 = 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>
  </head>
  <body onload="getDataNext()">
  <h2 id="XML-nbsp-Order-nbsp-Database">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>
  <input type="button" value="  <<  " onClick="getDataPrev();">  <input
 type="button" value="  >>  " onClick="getDataNext();">
  </form>  
  </body>
</html>

Run
This web page will pass in and run the initialization of the script. You must make sure that the order.xml document is in the same path as jsxml.html.

The initialization part instantiates a new ActiveX object as the MSXML2.DOMDocument.3.0 object type, and then the script passes the order.xml document into memory and selects all /Order/Item nodes. We use the /Order/Item node to identify the options that the document already contains.

The

standard in the document has an onLoad attribute, which enables the web page to initialize by calling getDataNext(). This feature can be used to get the next value from an XML document and display it in a form. We use a simple index to access specific options.

Both the forward (>>) and backward (

The above is the content of using JavaScript to access XML data. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function