Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM Browser Differences

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:


DOM - Whitespace and Line Breaks

XML often contains line breaks or whitespace characters between nodes. This often happens when editing documents with simple editors (such as Notepad).

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

Everyday Italian

Giada De Laurentiis

2005

30.00

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 »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Get the child nodes of the root element
  3. Output the number of child nodes. The result depends on the browser you use. IE will output 4 (alerting 4 child nodes), while other browsers will output 9 (alerting 9 child nodes).