Here you can find the source of getChildElements(Element parent, String elementTag)
Parameter | Description |
---|---|
parent | The parent element to get the children from |
elementTag | The tag of the elements to return |
public static List<Element> getChildElements(Element parent, String elementTag)
//package com.java2s; /******************************************************************************* * Copyright (c) 2014 ?cole Polytechnique de Montr?al * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/* w ww. j a v a2s .c o m*/ * Genevi?ve Bastien - Initial API and implementation *******************************************************************************/ import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Get only the XML element children of an XML element. * * @param parent * The parent element to get children from * @return The list of children Element of the parent */ public static List<Element> getChildElements(Element parent) { NodeList childNodes = parent.getChildNodes(); List<Element> childElements = new ArrayList<>(); for (int index = 0; index < childNodes.getLength(); index++) { if (childNodes.item(index).getNodeType() == Node.ELEMENT_NODE) { childElements.add((Element) childNodes.item(index)); } } return childElements; } /** * Get the XML children element of an XML element, but only those of a * certain type * * @param parent * The parent element to get the children from * @param elementTag * The tag of the elements to return * @return The list of children {@link Element} of the parent */ public static List<Element> getChildElements(Element parent, String elementTag) { /* get the state providers and find the corresponding one */ NodeList nodes = parent.getElementsByTagName(elementTag); List<Element> childElements = new ArrayList<>(); for (int i = 0; i < nodes.getLength(); i++) { Element node = (Element) nodes.item(i); if (node.getParentNode().equals(parent)) { childElements.add(node); } } return childElements; } }