Here you can find the source of getChildElements(Element parent, String tagName)
Parameter | Description |
---|---|
parent | The parent Element . |
tagName | The tag name of the child Element s to return. May be null. In this case all child Element s are returned. |
public static List<Element> getChildElements(Element parent, String tagName)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010-2015 BSI Business Systems Integration AG. * 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:/*from w ww . ja va 2 s. co m*/ * BSI Business Systems Integration AG - 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 { /** * Gets all child {@link Element}s of the given parent {@link Element}. * * @param parent * The parent element. * @return A {@link List} containing all child {@link Element} of the given parent {@link Element}. */ public static List<Element> getChildElements(Element parent) { return getChildElements(parent, null); } /** * Gets all child {@link Element}s of the given parent {@link Element} having the given tag name. * * @param parent * The parent {@link Element}. * @param tagName * The tag name of the child {@link Element}s to return. May be null. In this case all child {@link Element}s * are returned. * @return A {@link List} containing all child {@link Element} of given parent {@link Element} having the given tag * name. */ public static List<Element> getChildElements(Element parent, String tagName) { NodeList children = null; if (tagName == null) { children = parent.getChildNodes(); } else { children = parent.getElementsByTagName(tagName); } List<Element> result = new ArrayList<Element>(children.getLength()); for (int i = 0; i < children.getLength(); i++) { Node n = children.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { result.add((Element) n); } } return result; } }