Example usage for org.w3c.dom Node getNextSibling

List of usage examples for org.w3c.dom Node getNextSibling

Introduction

In this page you can find the example usage for org.w3c.dom Node getNextSibling.

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:Main.java

protected static void getTextFromNode(Node node, StringBuffer buffer, boolean addSpace) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        buffer.append(node.getNodeValue());
        if (addSpace)
            buffer.append(" ");
    }/*w  ww  . j  a  v  a 2s . c  om*/
    Node child = node.getFirstChild();
    while (child != null) {
        getTextFromNode(child, buffer, addSpace);
        child = child.getNextSibling();
    }
}

From source file:Main.java

/**
 * Determine whether an element has any child elements.
 * @param element the element to check./*from  w ww  .  ja v  a  2s  . c  o m*/
 * @return true if the element has child elements; false otherwise.
 */
public static boolean hasChildElements(Element element) {
    Node child = element.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE)
            return true;
        child = child.getNextSibling();
    }
    return false;
}

From source file:Utils.java

public static Element getFirstElement(Node parent) {
    Node n = parent.getFirstChild();
    while (n != null && Node.ELEMENT_NODE != n.getNodeType()) {
        n = n.getNextSibling();
    }/*from w ww .j a v  a2 s.  c  om*/
    if (n == null) {
        return null;
    }
    return (Element) n;
}

From source file:Main.java

public static Element findNextSiblingElement(Element current) {
    org.w3c.dom.Node ret = current.getNextSibling();
    while (ret != null) {
        if (ret instanceof Element) {
            return (Element) ret;
        }/*from  ww w . j a v  a2  s  . c  o  m*/
        ret = ret.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Returns the first XML child tag with the specified name.
 *
 * @param node The node to search children of
 * @param name The name of the node to search for, case-sensitive.
 * @return The child with the specified name, or null if none exists.
 *//*www  .  j a  v  a  2  s.  co m*/
public static Node getChildNode(Node node, String name) {
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeName().equals(name)) {
            return child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static String textContent(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        StringBuffer sb = new StringBuffer();
        Node nextChild = node.getFirstChild();
        while (nextChild != null) {
            sb.append(textContent(nextChild));
            nextChild = nextChild.getNextSibling();
        }//  www.  j  a  v  a2  s .com
        return sb.toString();
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        return node.getNodeValue();
    default:
        return "";
    }
}

From source file:Main.java

/** Finds and returns the next sibling node with the given name. */
public static Element getNextSiblingElement(Node node, String elemName) {

    if (node == null)
        return null;
    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            if (sibling.getNodeName().equals(elemName)) {
                return (Element) sibling;
            }/*from   ww w  . j  a  v a  2 s  .  co m*/
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static boolean hasOnlyTextChildren(/*@Nonnull*/Node node) {
    Node childNode = node.getFirstChild();

    while (childNode != null) {
        if (!(childNode instanceof Text)) {
            return false;
        }//  w  ww .  jav  a2  s  .c  o m

        childNode = childNode.getNextSibling();
    }

    return true;
}

From source file:Main.java

public static boolean hasOnlyTextChildren(@Nonnull Node node) {
    Node childNode = node.getFirstChild();

    while (childNode != null) {
        if (!(childNode instanceof Text)) {
            return false;
        }/*from  www . j a  v  a2  s  . c o m*/

        childNode = childNode.getNextSibling();
    }

    return true;
}

From source file:Main.java

/**
 * get the node following the node in parameter
 * /* w  w w. j a va2  s .c  om*/
 * @param n
 *            the n
 * @return the node
 */
public static Node next(Node n) {
    if (n == null) {
        return null;
    }
    Node currentNode = n;
    if (currentNode.getChildNodes().getLength() > 0) {
        currentNode = currentNode.getFirstChild();
    } else {
        if (currentNode.getNextSibling() != null) {
            currentNode = currentNode.getNextSibling();
        } else {
            Node oldCurrentNode = currentNode;
            currentNode = currentNode.getParentNode().getNextSibling();
            while (oldCurrentNode != null && currentNode == null) {
                oldCurrentNode = oldCurrentNode.getParentNode();
                if (oldCurrentNode != null) {
                    currentNode = oldCurrentNode.getNextSibling();
                }
            }
        }
    }
    return currentNode;
}