XML Responses and AJAX - Student Nodes Collection
(Page 5 of 5 )
We can get a reference to the student nodes using the getElementsByTagName method. From this we can find the number of such student nodes. Once we know how many there are (nodes.length), we can iterate through the collection and find the individuals as shown in the following code snippet.
var nodes=xhr.responseXML.getElementsByTagName("student");
document.getElementById(9).innerHTML=nodes.length;
for (i=0; i<nodes.length ;="" i="" {="" document.getelementbyid(10+i).innerhtml="nodes.item(i).text;" }=""></nodes.length>
Displayed result for the above snippet
4 Linda Jones Access, VB5.0
Adam Davidson Cobol, MainFrame
Charles Boyer HTML, Photoshop
Charles Mann Cobol, MainFrame
Relevant portion of the XML Document

Trapping error in XML document
Using the XMLDOM's parseError property the errant line in the XML document can be found and corrected. The code needed for this will be:
err = xhr.responseXML.parseError;
if (err.errorCode != 0)
alert("Error on line " + err.line + "\n" + err.srcText);
You may test it by making a malformed XML document by replacing the following line in the XML file WebClass.xml
<legacySkill>Cobol, MainFrame</legacySkill>with
<legacySkill>Cobol, MainFrame</legacySkiLl>
and the above code if placed in the source code will produce this prompt,
error information.

Summary
This tutorial looks at responseXML's parsing via XMLDOM properties and methods. The XML document must be well formed. Malformed XML documents retrieved using the above code will produce an error. However, if you are returning the responseText, the code still works. It is during XMLDOM parsing that the error is detected. You can verity this by trying to return responseText for a malformed XML Document. Again, JavaScript must be enabled and the script is case sensitive. This tutorial has only implemented the code for IE.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |