| home / programming / prof_ajax / 1 | [next] |
|
|
When XML data is loaded, errors can be thrown for a variety of reasons. For example, the external XML
file may not be found or the XML may not be well formed. To handle these occasions, MSXML provides
the parseError object, which contains the error information. This object is a property of every XML
DOM document MSXML creates.
To check for errors, the parseError object exposes the errorCode property, which can be compared to
the integer 0; if errorCode does not equal 0, an error has occurred. The following example is designed
specifically to cause an error:

In the highlighted line, notice that the <person/> element is not closed. Because the XML being loaded
is not well formed, an error occurs. The errorCode is then compared to 0; if they do not match (and
they don't in this example), an alert will display what caused the error. To do this, it uses the reason
property of the parseError object, which describes the reason for the error.
The parseError object provides the following properties to enable you to better understand an error:
|
Although all of these properties provide information about each error, it is up to you which ones make the most sense given your needs.

When it came time to implement the XML DOM in Mozilla Firefox, the developers took a more standards- centric approach in making it a part of the JavaScript implementation. In doing so, Mozilla ensured XML DOM support on all platforms in all Gecko-based browsers.
To create an XML DOM in Firefox, the createDocument() method of the document.implementation
object is called. This method takes three arguments: the first is a string containing the namespace URI for
the document to use, the second is a string containing the qualified name of the document’s root element,
and the third is the type of document (also called doctype) to create. To create an empty DOM document,
you can do this:
var oXmlDom = document.implementation.createDocument(“”, “”, null); |
By passing in an empty string for the first two arguments, and null for the last, you ensure a completely
empty document. In fact, there is currently no JavaScript support for doctypes in Firefox, so the third
argument must always be null. To create an XML DOM with a document element, specify the tag name
in the second argument:
var oXmlDom = document.implementation.createDocument(“”, “books”, null); |
This code creates an XML DOM whose documentElement is <books/>. You can take it a step further and
specify a namespace in the creation of the DOM by specifying the namespace URI in the first argument:

When a namespace is specified in the createDocument() method, Firefox automatically assigns the
prefix a0 to represent the namespace URI:
<a0:books xmlns:a0=”http://www.site1.com” /> |
From here, you can populate the XML document programmatically; generally, however, you will want to load preexisting XML documents into a blank XML DOM object.
Loading XML into an XML DOM is similar to Microsoft’s approach with one glaring difference: Firefox
supports only the load() method. Therefore, you can use the same code to load external XML data in
both browsers:
oXmlDom.load(“books.xml”); |
Also like Microsoft, Firefox implemented the async property, and its behavior matches that of Microsoft’s: setting async to false forces the document to be loaded in synchronous mode; otherwise, the document is loaded asynchronously.
Another difference between the Firefox and Microsoft XML DOM implementations is that Firefox does
not support the readyState property or the onreadystatechange event handler. Instead, it supports
the load event and the onload event handler. The load event fires after the document is completely
loaded:

As mentioned previously, the loadXML() method does not exist in the Firefox implementation; however,
it is possible to emulate the loadXML() behavior through the Firefox DOMParser class. This class has a
method called parseFromString(), which loads a string and parses it into a document:

In this code, a string of XML is created to pass to the DOMParser parseFromString() method. The two
arguments for parseFromString() are the XML string and the content type of the data (typically set to
“text/xml”). The parseFromString() method returns an XML DOM object, so you can treat oXmlDom
in this code as one.
Despite all their differences, IE and Firefox do share many properties and methods used to retrieve XML data contained in the document. As in IE, you can retrieve the root element of the document by using the documentElement property, as follows:
var oRoot = oXmlDom.documentElement; |
Firefox also supports the W3C standards properties of attributes, childNodes, firstChild,
lastChild, nextSibling, nodeName, nodeType, nodeValue, ownerDocument, parentNode, and
previousSibling. Unfortunately, Firefox does not support the Microsoft-proprietary text and xml
properties, but thanks to its flexibility, you can emulate their behavior.
As a quick recap, the text property returns the content of the node or the concatenated text of the current
node and its descendants. Therefore, not only does it return the text of the existing node, but also
the text of all child nodes; this is easy enough to emulate. A simple function that takes a node as an argument
can provide the same result:

In getText(), sText is used to store every piece of text that is retrieved. As the for loop iterates
through the oNode children, each child is checked to see if it contains children. If it does, the childNode
is passed through getText() and goes through the same process. If no children exist, then the
nodeValue of the current node is added to the string (for text nodes, this is just the text string). After all
children have been processed, the function returns sText.
The IE xml property serializes all XML contained in the current node. Firefox accomplishes the same
result by providing the XMLSerializer object. This object has a single method that is accessible using
JavaScript called serializeToString(). Using this method, XML data is serialized:

The serializeXml() function takes an XML node as an argument. An XMLSerializer object is created,
and the node is passed to the serializeToString() method. The result of this method, a string
representation of the XML data, is returned to the caller.
Firefox shares the same DOM methods for manipulating nodes as IE. Refer to the “Manipulating the DOM in IE” section for a refresher.
| home / programming / prof_ajax / 1 | [next] |
Created: March 27, 2003
March 6, 2006
URL: http://webreference.com/programming/prof_ajax/1