Java XML Child Element By QName getChildElement(Node parent, QName childNamespace)

Here you can find the source of getChildElement(Node parent, QName childNamespace)

Description

get Child Element

License

Apache License

Declaration

public static Element getChildElement(Node parent, QName childNamespace) 

Method Source Code


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

import org.w3c.dom.*;

import javax.xml.namespace.QName;

public class Main {
    public static Element getChildElement(Node parent, QName childNamespace) {
        if (parent != null) {
            NodeList children = parent.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                Node child = children.item(i);
                if (isElement(child) && matches(child, childNamespace.getLocalPart(), childNamespace)) {
                    return (Element) child;
                }/* w  w  w  .  j  a  v a  2  s.  c om*/
            }
        }
        return null;
    }

    private static boolean isElement(Node node) {
        return node.getNodeType() == Node.ELEMENT_NODE;
    }

    public static boolean matches(Node node, String requiredLocalName, QName requiredNamespace) {
        if (node == null) {
            return false;
        }
        boolean matchingNamespace = matchingNamespace(node, requiredNamespace);
        return matchingNamespace && matchingLocalName(node, requiredLocalName);
    }

    private static boolean matchingNamespace(Node node, QName requiredNamespace) {
        if (requiredNamespace == null) {
            return true;
        } else {
            return requiredNamespace.getNamespaceURI().equals(node.getNamespaceURI());
        }
    }

    private static boolean matchingLocalName(Node node, String requiredLocalName) {
        if (requiredLocalName == null) {
            return true;
        } else {
            String localName = node.getLocalName();
            return requiredLocalName.equals(localName);
        }
    }
}

Related

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