Microsoft XML Core Services (MSXML) 4.0 - DOM Reference

hasChildNodes Method

Provides a fast way to determine whether a node has children.

[Script]

Script Syntax

boolValue = oXMLDOMNode.hasChildNodes();

Return Value

Boolean. Returns True if this node has children.

Example

The following script example checks a node to see if it has any child nodes. If it does, it displays the number of child nodes it contains.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
var currNode;
xmlDoc.async = false;
xmlDoc.load("books.xml");
currNode = xmlDoc.documentElement.firstChild;
if (currNode.hasChildNodes()) {
  alert(currNode.childNodes.length);
} else {
  alert("no child nodes");
}
[Visual Basic]

Visual Basic Syntax

boolValue = oXMLDOMNode.hasChildNodes

Return Value

Boolean. Returns True if this node has children.

Example

The following Microsoft® Visual Basic® example checks a node to see if it has any child nodes. If it does, it displays the number of child nodes it contains.

Dim xmlDoc As New Msxml2.DOMDocument40
Dim currNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.Load ("books.xml")
Set currNode = xmlDoc.documentElement.firstChild
If currNode.hasChildNodes Then
  MsgBox currNode.childNodes.length
Else
  MsgBox "no child nodes"
End If
[C/C++]

C/C++ Syntax

HRESULT hasChildNodes(
    VARIANT_BOOL *hasChild);

Parameters

hasChild [out, retval]
Returns True if this node has children.

C/C++ Return Values

S_OK
The value returned if successful.
S_FALSE
The value returned when there are no children.
E_INVALIDARG
The value returned if hasChild is Null.

Remarks

It always returns False for nodes that, by definition, cannot have children: the IXMLDOMCDATASection, IXMLDOMComment, IXMLDOMNotation, IXMLDOMProcessingInstruction, and IXMLDOMText nodes.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Applies to: IXMLDOMAttribute | IXMLDOMCDATASection | IXMLDOMCharacterData | IXMLDOMComment | DOMDocument | IXMLDOMDocumentFragment | IXMLDOMDocumentType | IXMLDOMElement | IXMLDOMEntity | IXMLDOMEntityReference | IXMLDOMNode | IXMLDOMNotation | IXMLDOMProcessingInstruction | IXMLDOMText