Java XML Child Element By QName getChildElementsIntern(final Node node, final QName nodeName, final boolean recurse)

Here you can find the source of getChildElementsIntern(final Node node, final QName nodeName, final boolean recurse)

Description

get Child Elements Intern

License

Apache License

Declaration

private static List<Element> getChildElementsIntern(final Node node, final QName nodeName,
            final boolean recurse) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import javax.xml.namespace.QName;

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    private static List<Element> getChildElementsIntern(final Node node, final QName nodeName,
            final boolean recurse) {
        final ArrayList<Element> list = new ArrayList<Element>();
        if (node == null) {
            return list;
        }/*from  w ww . j a  v  a 2s  .c  om*/
        final NodeList nlist = node.getChildNodes();
        for (int i = 0; i < nlist.getLength(); i++) {
            final Node child = nlist.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if (nodeName == null) {
                    list.add((Element) child);
                } else {
                    QName qname;
                    if (nodeName.getNamespaceURI().length() > 0) {
                        qname = new QName(child.getNamespaceURI(), child.getLocalName());
                    } else {
                        qname = new QName(
                                child.getLocalName() == null ? child.getNodeName() : child.getLocalName());
                    }
                    if (qname.equals(nodeName)) {
                        list.add((Element) child);
                    }
                }
                if (recurse) {
                    list.addAll(getChildElementsIntern(child, nodeName, true));
                }
            }
        }
        return Collections.unmodifiableList(list);
    }
}

Related

  1. getChildElement(Element doc, QName elementQName)
  2. getChildElement(Node parent, QName childNamespace)
  3. getChildElementOrNull(QName qName, Element element)
  4. getChildElements(QName qName, Element element)
  5. getChildElementsAsListIntern(Node node, QName nodeName, boolean recursive)