Java XML Last Child Element getLastChildElement(Node n)

Here you can find the source of getLastChildElement(Node n)

Description

Gets the last child Element of the node, skipping any Text nodes such as whitespace.

License

Open Source License

Parameter

Parameter Description
n The parent in which to search for children

Return

The last child Element of n, or null if none

Declaration

public static Element getLastChildElement(Node n) 

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  www  .  j a v  a 2s. c  om
     * Gets the last child Element of the node, skipping any Text nodes such as
     * whitespace.
     * 
     * @param n
     *            The parent in which to search for children
     * @return The last child Element of n, or null if none
     */
    public static Element getLastChildElement(Node n) {
        Node child = n.getLastChild();
        while (child != null && child.getNodeType() != Node.ELEMENT_NODE)
            child = child.getPreviousSibling();
        if (child != null)
            return (Element) child;
        else
            return null;
    }

    /**
     * Gets the last 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 last child Element of n with the specified name, or null if
     *         none
     */
    public static Element getLastChildElement(Node n, String ns, String localName) {
        Element e = getLastChildElement(n);
        while (e != null && !isElementNamed(e, ns, localName))
            e = getPreviousSiblingElement(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 previous sibling Element of the node, skipping any Text nodes
     * such as whitespace.
     * 
     * @param n
     *            The sibling to start with
     * @return The previous sibling Element of n, or null if none
     */
    public static Element getPreviousSiblingElement(Node n) {
        Node sib = n.getPreviousSibling();
        while (sib != null && sib.getNodeType() != Node.ELEMENT_NODE)
            sib = sib.getPreviousSibling();
        if (sib != null)
            return (Element) sib;
        else
            return null;
    }

    /**
     * Gets the previous 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 previous sibling Element of n with the specified name, or
     *         null if none
     */
    public static Element getPreviousSiblingElement(Node n, String ns, String localName) {
        Element e = getPreviousSiblingElement(n);
        while (e != null && !isElementNamed(e, ns, localName))
            e = getPreviousSiblingElement(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. getLastChild(Element parent, String name)
  2. getLastChild(Node node, String name)
  3. getLastChildElement(Element e)
  4. getLastChildElement(Element e)
  5. getLastChildElement(Element elem)
  6. getLastChildElement(Node node)
  7. getLastChildElement(Node node)
  8. getLastChildElement(Node node)
  9. getLastChildElement(Node parent)