Web Services, Part VIII: Reading DTDs with JavaScript: Loading DTDs with JavaScript - Doc JavaScript
Web Services, Part VIII: Reading DTDs with JavaScript
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 <&month;> of 2001</description>
The DOM tree splits the text of this description
element to three nodes:
- Up to the entity reference:
"Sales Report for January, February, and <"
- Entity reference:
&month;
- Trail:
"> of 2001"
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
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: February 11, 2002
Revised: February 11, 2002
URL: https://www.webreference.com/js/column103/6.html