hasChildNodes Method

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - 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.5.0");
var currNode;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   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.DOMDocument50
Dim currNode As IXMLDOMNode
xmlDoc.async = False
xmlDoc.Load ("books.xml")
If (xmlDoc.parseError.errorCode <> 0) Then
   Dim myErr
   Set myErr = xmlDoc.parseError
   MsgBox("You have error " & myErr.reason)
Else
   Set currNode = xmlDoc.documentElement.firstChild
   If currNode.hasChildNodes Then
      MsgBox currNode.childNodes.length
   Else
      MsgBox "no child nodes"
   End If
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 the hasChild parameter 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