XML DOM firstChildProperty


Document object

Definition and Usage

The firstChild attribute returns the first child node of the document.

Syntax

documentObject.firstChild


Tips and notes

Note:Firefox and most other browsers treat empty spaces or line breaks generated between nodes as text nodes, while Internet Explorer ignores the blank text nodes generated between nodes. Therefore, in the following example, we will use a function to check the node type of the first child node.

The node type of an element node is 1. Therefore, if the first child node is not an element node, it moves to the next node and continues checking whether this node is an element node. This process continues until the first element child node is found. Through this method, we can get correct results in all browsers.

Tip:To learn more about browser differences, please visit our ... in our XML DOM tutorial.DOM browserChapter.


Example

The following code snippet usesloadXMLDoc()put "books.xmlLoad it into xmlDoc, and display the node name and node type of the document's first child node:

Example

//Get the name and value of the first child node function get_firstchild(n) { x=n.firstChild; while (x.nodeType!=1) { x=x.nextSibling; } return x; } xmlDoc=loadXMLDoc("books.xml"); x=get_firstchild(xmlDoc); document.write("Nodename: " + x.nodeName); document.write(" nodetype: " + x.nodeType + "<br>");

Output:

Nodename: bookstore (nodetype: 1)

Try it Yourself »

Try it Yourself

Get the last child node of the document


Document objectother extensions