import org.w3c.dom.*; // assume "element" is an XML element object NodeList childNodes = element.getChildNodes(); int numChildElements = 0; for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode.getNodeType() == Node.ELEMENT_NODE) { numChildElements++; } } System.out.println("Number of child elements: " + numChildElements);
import org.w3c.dom.*; // assume "parentElement" is an XML element object String tagName = "foo"; NodeList childNodes = parentElement.getElementsByTagName(tagName); System.out.println("Number of child nodes with tag name \"" + tagName + "\": " + childNodes.getLength());In this example, we obtain a list of child nodes of `parentElement` that have the tag name `"foo"` using the `getElementsByTagName()` method. We then print out the length of the resulting `NodeList`. Package library: This code example assumes that you have already obtained an `Element` object (e.g., by parsing an XML file using a library such as `javax.xml.parsers.DocumentBuilder`). This example does not specify which package or library is used for parsing.