spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column101


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

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Error Reporting during XML Parsing

The DOMDocument's parseError property returns an IXMLDOMParseError object that contains information about the last parsing error. The object's properties are:

As you can see, you get a wealth of information when a parsing error occurrs. Let's take our mydvd.xml file and introduce a syntax error in it. Let's change the line before the last from </data> to <data>. Let's write out the new XML file to a different file, say mydvdwitherror.xml, and now try reading it. An alert box will pop up and echo the IXMLDOMParseError object's properties. Here is the load() function that invokes the alert box:

function load() {
  var xmldoc;
  xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
  xmldoc.async = false;
  xmldoc.load("mydvdwitherror.xml");
  if (xmldoc.parseError.errorCode != 0) {
    alert("errorCode: " + xmldoc.parseError.errorCode + "\n" +
          "filepos: " + xmldoc.parseError.filepos + "\n" +
          "line: " + xmldoc.parseError.line + "\n" +
          "linepos: " + xmldoc.parseError.linepos + "\n" +
          "reason: " + xmldoc.parseError.reason + "\n" +
          "srcText: " + xmldoc.parseError.srcText + "\n" +
          "url: " + xmldoc.parseError.url);
  } else {
      alert(xmlDoc.documentElement.xml);
    }
}

Here is the XML file with the error:

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

Let your browser read the file now, and see which of the above information is used to show you where the error is. See how the browser shows you graphically the location of the error (position 5 in line 32).


Next: A Final Word

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


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/5.html