Scholar Repository
Home>Manual>XML DOM Browser Differences

XML DOM Browser Differences

From http://www.w3schools.com (Copyright Refsnes Data)

XML DOM Browser Differences

prev next


Different browsers handle empty text nodes in the XML DOM differently.


Examples

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


Browser Differences in DOM Parsing

All modern browsers support the W3C DOM specification.

However, there are some differences between browsers. Two important differences are:

  • The way they load XML
  • The way they handle white-spaces and new lines

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.


DOM - White Spaces and New Lines

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:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Get the child nodes of the root element
  3. Output the number of child nodes. The result is different depending on which browser you use. Firefox will alert 9 child nodes, while Internet Explorer will alert 4.

Try it yourself


Ignore Empty Text Between Elements

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:

  1. Load "books.xml" into xmlDoc using loadXMLDoc()
  2. Get the child nodes of the root element
  3. For each child node, check the node type of the node. If the node type is "1" it is an element node
  4. Output the name of the node if it is an element node

Try it yourself simple or Try it yourself full


prev next

From http://www.w3schools.com (Copyright Refsnes Data)

Home>Manual>XML DOM Browser Differences