XML DOM Browser differences


Browser differences in DOM parsing

All modern browsers support the W3C DOM specification.

However, there are differences between browsers. One important difference is:

  • Ways to handle whitespace and line breaks

DOM - Whitespace and line breaks

XML often contains line breaks or whitespace characters between nodes. This is often the case when editing documents with a simple editor, such as Notepad.

The following example (edited with Notepad) contains CR/LF (line breaks) between each line and two spaces before each child node:

<book>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

Internet Explorer will not treat empty whitespace or line breaks as text nodes, while other browsers will.

The following code snippet shows how many child nodes the root element (of books.xml) has:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.documentElement.childNodes;
document.write("Number of child nodes: " + x.length);

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xmlLoading in xmlDoc
  2. Get the child nodes of the root element
  3. Output the number of child nodes. The result depends on the browser you are using. IE will output 4 (indicating 4 child nodes), while other browsers will output 9 (indicating 9 child nodes).

other extensions