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:Utils.java

/**
 * Return the next sibling with a given name and type
 */// w  ww .  j  a va2  s  . c  om
public static Node getNext(Node current, String name, int type) {
    Node first = current.getNextSibling();
    if (first == null) {
        return null;
    }

    for (Node node = first; node != null; node = node.getNextSibling()) {

        if (type >= 0 && node.getNodeType() != type) {
            continue;
        }

        if (name == null) {
            return node;
        }
        if (name.equals(node.getNodeName())) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the first Element node in a NodeList
 * @param list the list to search//from  www  .  j  ava  2  s  .  co m
 * @return the element or null if none is found
 */
public static Element getFirstElement(NodeList list) {
    if (list.getLength() == 0) {
        return null;
    }

    Node node = list.item(0);
    while (node.getNodeType() != Node.ELEMENT_NODE) {
        if (node.getNextSibling() == null) {
            return null;
        }
        node = node.getNextSibling();
    }
    return (Element) node;
}

From source file:Main.java

public static Node getNext(final Node current, final String name, final int type) {
    Node next = current.getNextSibling();
    if (next == null) {
        return null;
    }//from ww  w.  j a  v a2 s  .  co m

    for (Node node = next; node != null; node = node.getNextSibling()) {
        if (type >= 0 && node.getNodeType() != type) {
            continue;
        } else {
            if (name == null) {
                return node;
            }
            if (name.equals(node.getNodeName())) {
                return node;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Element getNextSiblingElement(final Node node) {
    if (node != null) {
        Node tmpResult = node.getNextSibling();

        while (tmpResult != null) {
            if (Node.ELEMENT_NODE == tmpResult.getNodeType()) {
                return (Element) tmpResult;
            }/*  w  w  w . j  a  v  a 2  s  .  c o  m*/

            tmpResult = tmpResult.getNextSibling();
        }
    }

    return null;
}

From source file:Main.java

public static Element findFirstChildElement(Element parent) {
    org.w3c.dom.Node ret = parent.getFirstChild();
    while (ret != null && (!(ret instanceof Element))) {
        ret = ret.getNextSibling();
    }/*  w w  w .  j  av  a2 s  . c o m*/
    return (Element) ret;
}

From source file:Main.java

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

    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                if (sibling.getNodeName().equals(elemNames[i])) {
                    return (Element) sibling;
                }/*from w  w  w .  j  a va  2 s  .  c  om*/
            }
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

private static void removeEmptyTextNodes(Node parentNode) {
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        Node nextChild = childNode.getNextSibling();
        short nodeType = childNode.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            boolean containsOnlyWhitespace = childNode.getNodeValue().trim().isEmpty();
            if (containsOnlyWhitespace) {
                parentNode.removeChild(childNode);
            }/*from  w w  w .  j a v  a 2s.c o m*/
        }
        childNode = nextChild;
    }
}

From source file:Main.java

public static Node firstElementNodeChild(Node n) {
    Node m = n.getFirstChild();

    while (m != null && !isElementNode(m))
        m = m.getNextSibling();

    return m;/*from   ww  w.j a va  2 s .  c  o m*/
}

From source file:Main.java

public static Element getNextSibling(Element e) {
    Node n = e.getNextSibling();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getNextSibling();
    return (Element) n;
}

From source file:Main.java

/**
 * Gets the first element of a Node//from w  w w .ja  va2s  . c  o  m
 * @param parent Node
 * @return first element of a Node
 */
public static Element getFirstElement(Node parent) {
    Node n = parent.getFirstChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();
    }
    if (n == null) {
        return null;
    }
    return (Element) n;
}