Java XML First Child Element getFirstChildElement(Node n, String ns, String localName)

Here you can find the source of getFirstChildElement(Node n, String ns, String localName)

Description

Gets the first child Element of the node of the given name, skipping any Text nodes such as whitespace.

License

Open Source License

Parameter

Parameter Description
n The parent in which to search for children
ns The namespace URI of the element to locate
localName The local name of the element to locate

Return

The first child Element of n with the specified name, or null if none

Declaration

public static Element getFirstChildElement(Node n, String ns, String localName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**/*from w w  w  . j  a v  a  2s .  com*/
     * Gets the first child Element of the node, skipping any Text nodes such as
     * whitespace.
     * 
     * @param n
     *            The parent in which to search for children
     * @return The first child Element of n, or null if none
     */
    public static Element getFirstChildElement(Node n) {
        Node child = n.getFirstChild();
        while (child != null && child.getNodeType() != Node.ELEMENT_NODE)
            child = child.getNextSibling();
        if (child != null)
            return (Element) child;
        else
            return null;
    }

    /**
     * Gets the first child Element of the node of the given name, skipping any
     * Text nodes such as whitespace.
     * 
     * @param n
     *            The parent in which to search for children
     * @param ns
     *            The namespace URI of the element to locate
     * @param localName
     *            The local name of the element to locate
     * @return The first child Element of n with the specified name, or null if
     *         none
     */
    public static Element getFirstChildElement(Node n, String ns, String localName) {
        Element e = getFirstChildElement(n);
        while (e != null && !isElementNamed(e, ns, localName))
            e = getNextSiblingElement(e);
        return e;
    }

    /**
     * Shortcut for checking a DOM element node's namespace and local name
     * 
     * @param e
     *            An element to compare against
     * @param ns
     *            An XML namespace to compare
     * @param localName
     *            A local name to compare
     * @return true iff the element's local name and namespace match the
     *         parameters
     */
    public static boolean isElementNamed(Element e, String ns, String localName) {
        return (e != null && safeCompare(ns, e.getNamespaceURI()) && safeCompare(localName, e.getLocalName()));
    }

    /**
     * Gets the next sibling Element of the node, skipping any Text nodes such
     * as whitespace.
     * 
     * @param n
     *            The sibling to start with
     * @return The next sibling Element of n, or null if none
     */
    public static Element getNextSiblingElement(Node n) {
        Node sib = n.getNextSibling();
        while (sib != null && sib.getNodeType() != Node.ELEMENT_NODE)
            sib = sib.getNextSibling();
        if (sib != null)
            return (Element) sib;
        else
            return null;
    }

    /**
     * Gets the next sibling Element of the node of the given name, skipping any
     * Text nodes such as whitespace.
     * 
     * @param n
     *            The sibling to start with
     * @param ns
     *            The namespace URI of the element to locate
     * @param localName
     *            The local name of the element to locate
     * @return The next sibling Element of n with the specified name, or null if
     *         none
     */
    public static Element getNextSiblingElement(Node n, String ns, String localName) {
        Element e = getNextSiblingElement(n);
        while (e != null && !isElementNamed(e, ns, localName))
            e = getNextSiblingElement(e);
        return e;
    }

    /**
     * Compares two strings for equality, allowing for nulls
     * 
     * @param s1
     *            The first operand
     * @param s2
     *            The second operand
     * @return true iff both are null or both are non-null and the same strng
     *         value
     */
    public static boolean safeCompare(String s1, String s2) {
        if (s1 == null || s2 == null)
            return s1 == s2;
        else
            return s1.equals(s2);
    }
}

Related

  1. getFirstChildElement(Element parentElem)
  2. getFirstChildElement(Element root)
  3. getFirstChildElement(final Node node)
  4. getFirstChildElement(final Node parent, final String elemName)
  5. getFirstChildElement(Node n)
  6. getFirstChildElement(Node node)
  7. getFirstChildElement(Node node)
  8. getFirstChildElement(Node node)
  9. getFirstChildElement(Node node)