Here you can find the source of getChildElements(final Element element, final String localName)
Parameter | Description |
---|---|
element | the parent Element |
localName | the local name of the child Element s to retrieve |
public static List<Element> getChildElements(final Element element, final String localName)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; public class Main { /**/*from w w w .j a va 2 s . c om*/ * Retrieves all child {@link Element}s from a given parent {@link Element} * as {@link List}. * * @param element * the parent {@link Element} * @return the {@link List} of child {@link Element}s */ public static List<Element> getChildElements(final Element element) { final List<Element> result = new ArrayList<Element>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) { continue; } result.add((Element) child); } return result; } /** * Retrieves all child {@link Element}s of a given namespace URI and local * name from a given parent {@link Element} as {@link List}. * * @param element * the parent {@link Element} * @param namespaceURI * the namespace URI of the child {@link Element}s to retrieve * @param localName * the local name of the child {@link Element}s to retrieve * @return the respective {@link List} of child {@link Element}s */ public static List<Element> getChildElements(final Element element, final String namespaceURI, final String localName) { final List<Element> result = new ArrayList<Element>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) { continue; } if (namespaceURI != null && !namespaceURI.equals(child.getNamespaceURI())) { continue; } if (!localName.equals(child.getLocalName())) { continue; } result.add((Element) child); } return result; } /** * Retrieves all child {@link Element}s of a given local name from a given * parent {@link Element} as {@link List}. * * @param element * the parent {@link Element} * @param localName * the local name of the child {@link Element}s to retrieve * @return the respective {@link List} of child {@link Element}s */ public static List<Element> getChildElements(final Element element, final String localName) { final List<Element> result = new ArrayList<Element>(); for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) { if (!(child instanceof Element)) { continue; } if (!localName.equals(child.getNodeName())) { continue; } result.add((Element) child); } return result; } static String getLocalName(final Node node) { final String result = node.getLocalName(); return result != null ? result : node.getNodeName(); } }