Home arrow XML arrow Page 5 - XML Responses and AJAX
XML

XML Responses and AJAX


In a previous tutorial we looked at the responseText property of the XMLHTTP Request. This tutorial will focus on the responseXML accessor.

Author Info:
By: Jayaram Krishnaswamy
Rating: 4 stars4 stars4 stars4 stars4 stars / 40
February 27, 2006
TABLE OF CONTENTS:
  1. · XML Responses and AJAX
  2. · A brief explanation of innerHTML
  3. · AJAX returning an XML document
  4. · A Quick look at XMLDOM
  5. · Student Nodes Collection

print this article
SEARCH DEVARTICLES

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.

blog comments powered by Disqus
XML ARTICLES

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials