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

public static Element getFirstChildElement(Node node) {
    Node child = node.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
        child = child.getNextSibling();
    }//from w  w w .j a  va  2s. c  o  m
    return (Element) child;
}

From source file:DOMHelper.java

/**
 * Gets the first child element of a node.
 * @param node the node to get the child from
 * @return the first element child of {@code node} or {@code null} if none
 * @throws NullPointerException if {@code node} is {@code null}
 */// w w w.  j av a2  s.c om
public static Element getFirstChildElement(Node node) {
    node = node.getFirstChild();
    while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
        node = node.getNextSibling();
    }
    return (Element) node;
}

From source file:Signing.java

  public static Element getNextSiblingElement(Node node) {
  Node sibling = node.getNextSibling();
  while ((sibling != null) && (sibling.getNodeType() != Node.ELEMENT_NODE)) {
    sibling = sibling.getNextSibling();/* w w  w  .j  a  va 2  s. c om*/
  }
  return (Element) sibling;
}

From source file:Main.java

/** Look for Element node.
 *
 *  <p>Checks the node and its siblings.
 *  Does not descent down the 'child' links.
 *
 *  @param node Node where to start./* ww w  . j a va  2s. c  o  m*/
 *  @return Returns node, next Element sibling or <code>null</code>.
 */
public static final Element findElement(Node node) {
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Returns the next Element node.//from ww  w  .ja  v  a 2s  .co m
 */
private static Element getNextElementNode(Node node) {
    while (node != null) {
        if (Node.ELEMENT_NODE == node.getNodeType())
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}

From source file:Main.java

private static void cleanEmptyTextNodes(Node parentNode) {
    boolean removeEmptyTextNodes = false;
    Node childNode = parentNode.getFirstChild();
    while (childNode != null) {
        removeEmptyTextNodes |= checkNodeTypes(childNode);
        childNode = childNode.getNextSibling();
    }//from  w ww .ja  v  a 2s .  c om

    if (removeEmptyTextNodes) {
        removeEmptyTextNodes(parentNode);
    }
}

From source file:Main.java

public static Element getNextElement(Element el) {
    Node node = el.getNextSibling();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getNextSibling();
    }//  w  ww .java 2s  .c  o m
    return null;
}

From source file:Main.java

/** Finds and returns the next sibling element node. */
public static Element getNextSiblingElement(Node node) {

    if (node == null)
        return null;
    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) sibling;
        }/*from   w  w  w . j  a  va  2s  .  com*/
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/** Goes through and adds newlines and indent to the current node and all its children
 * @param current the current node//from   w  w w.ja v a 2s  . co  m
 * @param indent the current level of indent this is increased recursively*/
private static void addFormatting(Document doc, Node current, String indent) {

    // go through each of the children adding space as required
    Node child = current.getFirstChild();
    String childIndent = indent + "\t";
    while (child != null) {
        Node nextChild = child.getNextSibling();
        if (child.getNodeType() != Node.TEXT_NODE) {
            // Only if we aren't a text node do we add the space
            current.insertBefore(doc.createTextNode("\n" + childIndent), child);
            if (child.hasChildNodes()) {
                addFormatting(doc, child, childIndent);
            }
            if (nextChild == null) {
                // Because this is the last child, we need to add some space after it
                current.appendChild(doc.createTextNode("\n" + indent));
            }
        }
        child = nextChild;
    }
}

From source file:Main.java

public static Element getRootElement(Document doc) {
    Node node = doc.getFirstChild();
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getNextSibling();
    }/*  ww w .  j  a  v  a  2 s  .  c  om*/
    return null;
}