Java XML Child Get by Name getChildElementsByName(Element element, String localName)

Here you can find the source of getChildElementsByName(Element element, String localName)

Description

get Child Elements By Name

License

Open Source License

Declaration

public static List<Element> getChildElementsByName(Element element, String localName) 

Method Source Code

//package com.java2s;
/**//from  w  w  w  . j  ava 2s.  c o  m
 * This file belongs to the BPELUnit utility and Eclipse plugin set. See enclosed
 * license file for more information.
 */

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 {
    public static List<Element> getChildElementsByName(Element element, String localName) {
        List<Element> elements = new ArrayList<Element>();

        for (Element e : getChildElements(element)) {
            // DOM Level 1 API (document.createElement) creates elements
            // that have an empty localname
            if (e.getLocalName() == null && localName.equals(e.getNodeName())) {
                elements.add(e);
            } else if (localName.equals(e.getLocalName())) {
                elements.add(e);
            }
        }

        return elements;
    }

    public static List<Element> getChildElements(Element element) {
        NodeList childNodes = element.getChildNodes();

        List<Element> elements = new ArrayList<Element>();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node n = childNodes.item(i);
            if (n instanceof Element) {
                elements.add((Element) n);
            }
        }
        return elements;
    }
}

Related

  1. getChildElements(Element parent, String nsUri, String localPart)
  2. getChildElements(Element parent, String tagName)
  3. getChildElements(Element parent, String tagName)
  4. getChildElements(Element parent, String tagName)
  5. getChildElements(Element parent, String tagName)
  6. getChildElementsByName(Element parent, String elemName)
  7. getChildElementsByName(Element parent, String name)
  8. getChildElementsByName(Element parent, String name)
  9. getChildElementsByName(Element parent, String tagName)