From http://www.w3schools.com (Copyright Refsnes Data)
Different browsers handle empty text nodes in the XML DOM differently.
The examples below use the XML file
books.xml.
A function, loadXMLDoc(), in an external JavaScript is used to load the XML file.
Display the length of a node list
This example shows the length of a node list. The result is different in
Internet Explorer and other browsers
Ignore empty text between nodes
This example checks the nodeType of the nodes and only processes element nodes
All modern browsers support the W3C DOM specification.
However, there are some differences between browsers. Two important differences are:
The different ways to load XML is explained in the chapter "DOM Parser".
The different ways to handle white spaces and new lines is explained in this chapter.
XML often contains new line, or white space characters, between nodes. This is often the case when the document is edited by a simple editor like Notepad.
The following example (edited by Notepad) contains CR/LF (new line) between each line and two spaces in front of each child node:
<book> <title>Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> |
Firefox, and some other browsers, will treat empty white-spaces or new lines as text nodes, Internet Explorer will not
The following code fragment displays how many child nodes the root element (of books.xml) has:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement.childNodes; document.write("Number of child nodes: " + x.length); |
Example explained:
To ignore empty text nodes between element nodes, you can check the node type. An element node has type 1:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++) { if (x[i].nodeType==1) {// only process element nodes document.write(x[i].nodeName); document.write("<br />"); } } |
Example explained:
Try it yourself simple or Try it yourself full
From http://www.w3schools.com (Copyright Refsnes Data)