Here you can find the source of getChildNodes(Node rootNode, String childName)
Parameter | Description |
---|---|
rootNode | The root node |
childName | The name of child nodes |
public static Node[] getChildNodes(Node rootNode, String childName)
//package com.java2s; /*//from w ww .jav a 2s.c om * utils - DOMUtil.java - Copyright ? 2006-2009 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.ArrayList; import java.util.List; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Returns all child nodes of the given root node with the given name. * * @param rootNode * The root node * @param childName * The name of child nodes * @return All child nodes with the given name, or an empty array if there * are no child nodes with the given name */ public static Node[] getChildNodes(Node rootNode, String childName) { List<Node> nodes = new ArrayList<Node>(); NodeList nodeList = rootNode.getChildNodes(); for (int nodeIndex = 0, nodeSize = nodeList.getLength(); nodeIndex < nodeSize; nodeIndex++) { Node childNode = nodeList.item(nodeIndex); if (childNode.getNodeName().equals(childName)) { nodes.add(childNode); } } return nodes.toArray(new Node[nodes.size()]); } }