Java XML Node Sibiling getNextSibling(Node node)

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

Description

get Next Sibling

License

Open Source License

Declaration

public static Node getNextSibling(Node node) 

Method Source Code


//package com.java2s;
/*//ww w  . j  a va  2 s .  c o m
 * ? Copyright IBM Corp. 2011
 * 
 * Licensed 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 java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.w3c.dom.Node;

public class Main {
    private static Pattern _whitespacePattern;

    public static Node getNextSibling(Node node) {

        // skip empty text nodes.
        Node sibling = node.getNextSibling();
        while (sibling != null && isWhitespace(sibling)) {
            sibling = sibling.getNextSibling();
        }

        return sibling;
    }

    /**
     * test if a node is a text node that contains just whitespace.
     * 
     * @param node
     * @return true if the node contains all whitespace, false otherwise.
     */
    public static boolean isWhitespace(Node node) {

        if (node != null && node.getNodeType() == Node.TEXT_NODE) {
            String text = node.getNodeValue();
            Matcher m = _whitespacePattern.matcher(text);
            if (m.matches()) {
                return true;
            }
        }

        return false;
    }

    /**
     * test if a string to see if it contains just whitespace.
     * 
     * @param String
     * @return true if the string contains all whitespace, false otherwise.
     */
    public static boolean isWhitespace(String text) {

        if (text != null) {
            Matcher m = _whitespacePattern.matcher(text);
            if (m.matches()) {
                return true;
            }
        }

        return false;
    }
}

Related

  1. getNextElementSibling(Node node)
  2. getNextElementSibling(Node node)
  3. getNextHomoSibling(Node aNode)
  4. getNextNamedSibling(Node node, String nodeName)
  5. getNextNodeSibling(Node node)
  6. getNextSiblingElement(Node n)
  7. getNextSiblingElement(Node n, String ns, String localName)
  8. getNextSiblingElement(Node node)
  9. getNextSiblingElement(Node node)