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:
- the way whitespace and line breaks are handled
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:
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:
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Get the child nodes of the root element
- 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).