The XMLDocument Object
The responseXML
property of the XMLHttpRequest
object expects the return value of the remote call to be in the form of an XMLDocument
object. This requires the server code to return well-formed XML data so that the client script can parse it. However, it is easy to access this XML data; you have full DOM support for doing so.
JavaScript supports a set of DOM features to access specific nodes in the XML file or to navigate the tree structure of the XML document. Appendix B contains a complete list of methods and properties of the XMLDocument
object. The following example shows how to use quite a lot of them.
Imagine that the return data of the server request is the following XML data:
<book title="Programming Atlas" author="Christian Wenz"> <chapters> <chapter number="1" title="Introduction" /> <chapter number="2" title="JavaScript" /> <chapter number="3" title="Ajax" /> </chapters> </book>
Warning
It is important that when XML is returned, the Content-type
HTTP header of the response is explicitly set to "text/xml"
. If this header is omitted, some browsers (most notably, Mozilla and derivatives) refuse to parse the return data, and the responseXML
object is set to null
. The following C# code in an ASP.NET page shows how to set the content type appropriately:
void Page_Load() { if (Request.QueryString["sendData"] != null && Request.QueryString["sendData"] == "ok") { string xml = "<book title=\"Programming Atlas\" author=\"Christian Wenz\"><chapters><chapter ...
Get Programming Atlas now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.