Java XML Node Sibiling getNextSiblingElement(Node n, String ns, String localName)

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

Description

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

License

Open Source License

Parameter

Parameter Description
n The sibling to start with
ns The namespace URI of the element to locate
localName The local name of the element to locate

Return

The next sibling Element of n with the specified name, or null if none

Declaration

public static Element getNextSiblingElement(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 {
    /**//  www  . j a va  2s  .  co m
     * 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;
    }

    /**
     * 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()));
    }

    /**
     * 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. getNextHomoSibling(Node aNode)
  2. getNextNamedSibling(Node node, String nodeName)
  3. getNextNodeSibling(Node node)
  4. getNextSibling(Node node)
  5. getNextSiblingElement(Node n)
  6. getNextSiblingElement(Node node)
  7. getNextSiblingElement(Node node)
  8. getNextSiblingElement(Node node)
  9. getNextSiblingElement(Node node)