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 getFirstChild(Element e) {
    if (e == null)
        return null;
    Node n = e.getFirstChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getNextSibling();
    return (Element) n;
}

From source file:Main.java

/** Finds and returns the next sibling node with the given qualified name. */
public static Element getNextSiblingElementNS(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++) {
                String uri = sibling.getNamespaceURI();
                if (uri != null && uri.equals(elemNames[i][0])
                        && sibling.getLocalName().equals(elemNames[i][1])) {
                    return (Element) sibling;
                }/*  w  w w.  j  av a2 s  . c  om*/
            }
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Remove empty CDATA sections from the given node and all of its descendants. Some parsers
 * silently discard empty CDATA sections, and this method may be used to compare the output from
 * parsers that behave differently with respect to empty CDATA sections.
 * //from w ww .j a  va 2  s. com
 * @param node
 *            the node to process
 */
public static void removeEmptyCDATASections(Node node) {
    Node child = node.getFirstChild();
    while (child != null) {
        Node next = child.getNextSibling();
        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
            if (child.getNodeValue().length() == 0) {
                child.getParentNode().removeChild(child);
            }
            break;
        case Node.ELEMENT_NODE:
            removeEmptyCDATASections(child);
        }
        child = next;
    }
}

From source file:Main.java

/**
 * Get the indexed Element.//from   w  ww.ja va 2  s  .c om
 * <p>
 * Similar to <code>Element.getChildNodes.item</code>, but ignores any Nodes (such as
 * indentation TextNodes).
 */

public static Element getFirstChildElement(Element parent) {

    Node node = parent.getFirstChild();

    while (node != null && !(node instanceof Element)) {

        node = node.getNextSibling();
    }

    return (Element) node;
}

From source file:Main.java

/**
 * Static helper function to get the element data of the specified node.
 * //from   w  ww . j ava 2  s . com
 * @param node
 *            the node where the text data resides; may be <code>null</code>
 *            in which case this funtion will return ""
 * 
 * @return the complete text of the specified node, or an empty string if
 *         the node has no text or is <code>null</code>
 */
public static String getElementData(final Node node) {
    StringBuffer ret = new StringBuffer();

    if (node != null) {
        Node text;
        for (text = node.getFirstChild(); text != null; text = text.getNextSibling()) {
            /**
             * the item's value is in one or more text nodes which are its
             * immediate children
             */
            if (text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE) {
                ret.append(text.getNodeValue());
            } else {
                if (text.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                    ret.append(getElementData(text));
                }
            }
        }
    }
    return ret.toString();
}

From source file:Main.java

/**
 * Gets the first child element of an element
 *
 * @param element the parent//from ww  w.j av a 2 s  .  c o m
 * @return the first child element or null if there isn't one
 */
public static Element getFirstChildElement(Element element) {
    Node child = element.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE)
        child = child.getNextSibling();

    return (Element) child;
}

From source file:Utils.java

/**
 * Search for a named child of a given node
 * @param currentNode Starting point for our search
 * @param tagName Node name to look up/*from  www . j a va2 s  .c o m*/
 * @return matching Node (null if none)
 */
public static Node getChildSiblingByName(Node currentNode, String tagName) {
    Node node = currentNode.getFirstChild();

    while ((node != null) && (!node.getNodeName().equals(tagName))) {
        node = node.getNextSibling();
    }
    return node;
}

From source file:Main.java

/**
 * Returns the first XML child tag with the specified name.
 * /*from  w w  w  .java2 s.c  om*/
 * @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.
 */
public static Node getChildNode(Node node, String name) {
    Node child = node.getFirstChild();
    while (child != null && !child.getNodeName().equals(name)) {
        child = child.getNextSibling();
    }
    return child;
}

From source file:Main.java

/**
 * Get the next right sibling that is an element.
 *//*from w w w .j a  va  2s .  c  om*/
public static Element getNextElement(Node node) {

    Element found = null;

    if (node != null) {

        for (Node next = node.getNextSibling(); next != null && found == null; next = next.getNextSibling()) {

            if (next.getNodeType() == Node.ELEMENT_NODE) {
                found = (Element) next;
            }
        }
    }

    return found;
}

From source file:Main.java

public static Node findSibling(Node node, String tagName) {
    Node n = node;
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE && !n.getNodeName().equals(tagName))
        n = n.getNextSibling();
    return n;//from   w  w w .j av  a  2s.  co  m
}