spacer

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

home / experts / javascript / column103


Web Services, Part VIII: Reading DTDs with JavaScript

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


Loading DTDs with JavaScript

When reading XML with JavaScript, you cannot use the DTD file to define entity references only. Once you use the DTD file (in this case for the entity reference substitutions), you have to define the whole document structure, as we have showed you on Pages 2 and 3.

When loading XML files, it's better to keep error handling in place. The following script reads mydvd7.xml, prints a relevant error message if the parsing was not successful, and prints the DOM tree otherwise:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
var namedNodeMap;
xmlDoc.async = false;
xmlDoc.load("mydvd7.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);
  }

Try loading mydvd7.xml. You can see that the entity references were not substituted by their values from the DTD. Instead, their values got loaded as children of the entity references. April is now a child of &month;, and John Smith is now a child of &preparedby;. But how can April be a child of a substring of description? How are the nodes arranged?

Here is the description element again:

<description>Sales Report for January, February, 
  and &lt;&month;&gt; of 2001</description>

The DOM tree splits the text of this description element to three nodes:

Let's show that indeed this is the case. First, we find the list of description objects:

objNodeList = xmlDoc.getElementsByTagName("description");

Our description is the first and only instance, so its index is 0. The following script prints the text values of description's three children:

alert(objNodeList(0).childNodes(0).text);
alert(objNodeList(0).childNodes(1).text);
alert(objNodeList(0).childNodes(2).text);

When running this script, you should get the three strings above, where the entity reference &month; is substituted by April.

Similarly, we find the list of author objects:

objNodeList = xmlDoc.getElementsByTagName("author");

Our author is the first and only instance, so its index is 0. The following script prints the text values of author's two children:

alert(objNodeList(0).childNodes(0).text);
alert(objNodeList(0).childNodes(1).text);

When running this script, you should get two stings: "Author: " and "John Smith".


Next: How to add entity references on the fly

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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

URL: http://www.webreference.com/js/column103/6.html