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 removeElementContentWhitespace(Element root) {
    boolean foundElt = false;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        foundElt = true;//from   w  w  w . j  a v a 2s  .  com
        removeElementContentWhitespace(Element.class.cast(n1));
    }
    if (foundElt) {
        Node n1 = root.getFirstChild();
        while (n1 != null) {
            Node n2 = n1.getNextSibling();
            if (n1.getNodeType() == Node.TEXT_NODE && isEmptyText(Text.class.cast(n1))) {
                root.removeChild(n1);
            }
            n1 = n2;
        }
    }
    return root;
}

From source file:Signing.java

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

From source file:Main.java

/**
 * Returns the first direct child element for the given parent node, which
 * matches the given tagName (probably faster than retrieving all children first).
 * @param parent the parent node under which to search for the element
 * @param tagName the tag name of the element to find
 * @return Element - the first found Element or null if no such Element is present
 *//*from w ww.  j a  va  2 s. c om*/
public static Element findFirstChildElement(Node parent, String tagName) {
    Node n = parent.getFirstChild();
    do {
        if ((n.getNodeType() == Node.ELEMENT_NODE) && (n.getNodeName().equals(tagName))) {
            return (Element) n;
        }
        // Android Workaround
        try {
            n = n.getNextSibling();
        } catch (IndexOutOfBoundsException e) {
            n = null;
        }
    } while (n != null);
    return null;
}

From source file:NodeUtils.java

/**
 * Returns a first child DOM Node of type ELEMENT_NODE
 * for the specified Node.//from w ww  . j  a  v  a 2  s  . c  om
 */
public static Node getChildElementNode(Node xmlNode) {
    if (xmlNode == null || !xmlNode.hasChildNodes()) {
        return null;
    }

    xmlNode = xmlNode.getFirstChild();
    while (xmlNode != null && xmlNode.getNodeType() != Node.ELEMENT_NODE) {
        xmlNode = xmlNode.getNextSibling();
    }

    return xmlNode;
}

From source file:Main.java

public static String getNodeValue(final Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Node n = node.getFirstChild();
        if (n != null) {
            do {//from  w  ww. j a  v  a2 s . c  o m
                if (n.getNodeType() == Node.TEXT_NODE) {
                    return n.getNodeValue();
                }
            } while ((n = n.getNextSibling()) != null);
        }
    }

    return node.getNodeValue();
}

From source file:Main.java

public static List<Element> getNamedChildElements(final Element parent, final String name) {
    List<Element> elements = new ArrayList<Element>();
    if (parent != null) {
        Node child = parent.getFirstChild();
        while (child != null) {
            if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equals(name))) {
                elements.add((Element) child);
            }//from w  w w  .j a v a2  s. co m
            child = child.getNextSibling();
        }
    }
    return elements;
}

From source file:DomUtil.java

/**
 * Return the next sibling with a given name and type
 *//*ww  w  .j av  a2  s .co  m*/
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;
        // System.out.println("getNode: " + name + " " + node.getNodeName());
        if (name == null)
            return node;
        if (name.equals(node.getNodeName())) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

public static List childNodeList(Node node) {
    if (node == null)
        return null;

    List children = new LinkedList();
    Node childNode = node.getFirstChild();
    if (childNode != null) {
        do {/*from w  w w. j  a  v  a  2  s .co m*/
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                children.add(childNode);
            }
        } while ((childNode = childNode.getNextSibling()) != null);
    }

    return children;
}

From source file:Main.java

public static String text(Node node) {
    StringBuffer sb = new StringBuffer();
    Node n = node.getFirstChild();
    while (n != null) {
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE)
            sb.append(n.getNodeValue());
        n = n.getNextSibling();
    }/*from w  ww .j  av a  2  s .  c  o m*/
    return sb.toString();
}

From source file:Main.java

public static Element getFirstElementChild(final Element element, final String namespace,
        final String localname) {
    Node node = element.getFirstChild();
    if (node == null) {
        return null;
    }//from w w  w  .  j  a  v  a 2 s. co m

    do {
        if (match(node, namespace, localname)) {
            return (Element) node;
        }
    } while ((node = node.getNextSibling()) != null);
    return null;
}