Java XML Element Sibling getNextSiblingElement(Node node)

Here you can find the source of getNextSiblingElement(Node node)

Description

Returns the next sibling element of the specified node, or null if there is no such element.

License

Apache License

Parameter

Parameter Description
node the node

Exception

Parameter Description
NullPointerException if <code>node == null</code>

Return

the next sibling element of the specified node, or null if there is no such element

Declaration

public static Element getNextSiblingElement(Node node) 

Method Source Code

//package com.java2s;
/**//from  w  w  w .j  a  v a 2s  .c o m
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

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

import javax.xml.crypto.*;

public class Main {
    /**
     * Returns the next sibling element of the specified node, or null if there
     * is no such element.
     *
     * @param node the node
     * @return the next sibling element of the specified node, or null if there
     *    is no such element
     * @throws NullPointerException if <code>node == null</code>
     */
    public static Element getNextSiblingElement(Node node) {
        Node sibling = node.getNextSibling();
        while (sibling != null && sibling.getNodeType() != Node.ELEMENT_NODE) {
            sibling = sibling.getNextSibling();
        }
        return (Element) sibling;
    }

    /**
     * Returns the next sibling element of the specified node and checks that
     * the local name is equal to {@code localName}.
     *
     * @param node the node
     * @return the next sibling element of the specified node
     * @throws NullPointerException if {@code node == null}
     * @throws MarshalException if no such element or the local name is not
     * equal to {@code localName}
     */
    @Deprecated
    public static Element getNextSiblingElement(Node node, String localName) throws MarshalException {
        return verifyElement(getNextSiblingElement(node), localName);
    }

    /**
     * Returns the next sibling element of the specified node and checks that
     * the local name is equal to {@code localName} and the namespace is equal to
     * {@code namespaceURI}
     *
     * @param node the node
     * @return the next sibling element of the specified node
     * @throws NullPointerException if {@code node == null}
     * @throws MarshalException if no such element or the local name is not
     * equal to {@code localName}
     */
    public static Element getNextSiblingElement(Node node, String localName, String namespaceURI)
            throws MarshalException {
        return verifyElement(getNextSiblingElement(node), localName, namespaceURI);
    }

    private static Element verifyElement(Element elem, String localName) throws MarshalException {
        if (elem == null) {
            throw new MarshalException("Missing " + localName + " element");
        }
        String name = elem.getLocalName();
        if (!name.equals(localName)) {
            throw new MarshalException("Invalid element name: " + name + ", expected " + localName);
        }
        return elem;
    }

    private static Element verifyElement(Element elem, String localName, String namespaceURI)
            throws MarshalException {
        if (elem == null) {
            throw new MarshalException("Missing " + localName + " element");
        }
        String name = elem.getLocalName();
        String namespace = elem.getNamespaceURI();
        if (!name.equals(localName) || namespace == null && namespaceURI != null
                || namespace != null && !namespace.equals(namespaceURI)) {
            throw new MarshalException("Invalid element name: " + namespace + ":" + name + ", expected "
                    + namespaceURI + ":" + localName);
        }
        return elem;
    }
}

Related

  1. getNextSiblingElement(Element el)
  2. getNextSiblingElement(Element elem)
  3. getNextSiblingElement(Element elem)
  4. getNextSiblingElement(Element element)
  5. getNextSiblingElement(Element element)
  6. getNextSiblingElementByTagName(Element e, String name)
  7. getNextSiblingElementWithName(Element elem, String name)
  8. getPrecedingSiblings(Element element, String namespaceUri)
  9. getPreviousSiblingElement(Element elem)