Web Services, Part VI: XML Parsing and Loading from JavaScript: XML Loading - Doc JavaScript | WebReference

Web Services, Part VI: XML Parsing and Loading from JavaScript: XML Loading - Doc JavaScript


Web Services, Part VI: XML Parsing and Loading from JavaScript

XML Loading

When creating and consuming Web services, you need to be familiar with XML and operations on XML. In particular, you need to know how to load an XML file from a JavaScript script, and how to manipulate the data loaded into the browser. The data read from an XML file is arranged in memory as an object, the DOMDocument object. This DOMDocument object has 35 properties, 25 methods, and 3 events. With this object, you can load XML files, navigate the document graph model as shown on Page 2, query nodes of the graph, etc. The first thing you need to do is to create an empty object. You do it with the ActiveXObject creator:

xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");

You load an XML file with the load() method:

xmldoc.load("mydvd.xml");

The following script loads an XML file and prints its content to an alert box:

<SCRIPT LANGUAGE="JavaScript">
<!--
  var xmldoc=new ActiveXObject("MSXML2.DOMDocument.3.0");
  xmldoc.load("mydvd.xml");
  alert(xmldoc.documentElement.xml);
// -->
</SCRIPT>

The XML file looks like this:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="mydvd.xsl"?>
  <sales>
    <summary>
      <heading>MyDVD Rental Store</heading>
      <subhead>Periodical Sales Report</subhead>
      <description>Sales Report for January, February,
        and March of 2001</description>
    </summary>
     <data>
      <month>
        <name>January 2001</name>
        <week number="1" dvds_rented="12000" />
        <week number="2" dvds_rented="15000" />
        <week number="3" dvds_rented="18000" />
        <week number="4" dvds_rented="11800" />
      </month>
      <month>
        <name>February 2001</name>
        <week number="1" dvds_rented="11500" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="19050" />
        <week number="4" dvds_rented="11200" />
      </month>
      <month>
        <name>March 2001</name>
        <week number="1" dvds_rented="15300" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="10050" />
        <week number="4" dvds_rented="11230" />
      </month>
    </data>
  </sales>

The alert box will look like this:

Try it yourself. You should get an identical alert box.

Next: How to monitor XML parsing and building

http://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: January 14, 2002
Revised: January 14, 2002

URL: http://www.webreference.com/js/column101/3.html