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 getNextSiblingElement(Element element) {

    try {//from   ww  w .  ja  va2s . c  o m
        Node node = element.getNextSibling();

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

            node = node.getNextSibling();
        }

        return (Element) node;

    } catch (IndexOutOfBoundsException e) {

        // Android 1.6 throws IndexOutOfBoundsException instead of correctly returning null

        return null;
    }
}

From source file:Main.java

/**
 * Remove all children with specified name from the specified node
 */// ww w . jav a 2s .  c  om
public static void removeChildren(Node node, String name) {
    Node currentChild = node.getFirstChild();

    while (currentChild != null) {
        final Node nextChild = currentChild.getNextSibling();

        if (currentChild.getNodeName().equals(name))
            node.removeChild(currentChild);

        currentChild = nextChild;
    }
}

From source file:Main.java

/**
 * Finds and returns the next sibling node with the given name and
 * attribute name, value pair. Since only elements have attributes,
 * the node returned will be of type Node.ELEMENT_NODE.
 *///from  w  ww  .j  a  v  a 2  s . c  o m
public static Element getNextSiblingElement(Node node, String elemName, String attrName, String attrValue) {

    // search for node
    Node sibling = node.getNextSibling();
    while (sibling != null) {
        if (sibling.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) sibling;
            if (element.getNodeName().equals(elemName) && element.getAttribute(attrName).equals(attrValue)) {
                return element;
            }
        }
        sibling = sibling.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static Node insertNewAfter(final Document impD, final Node targetE, final Node newE) {
    Node impNewNode = impD.importNode(newE, true);
    Node parent = targetE.getParentNode();
    if (targetE.getNextSibling() == null) {
        parent.appendChild(impNewNode);//from  ww  w  .j  a  v a 2s .  c om
    } else {
        parent.insertBefore(impNewNode, targetE.getNextSibling());
    }
    return impNewNode;
}

From source file:Main.java

public static Node insertTextNodeAfter(String text, Node sibling) {
    Text newNode = sibling.getOwnerDocument().createTextNode(text);
    return sibling.getParentNode().insertBefore(newNode, sibling.getNextSibling());
}

From source file:Main.java

/**
 * Gets the first child Element of the node, skipping any Text nodes such as whitespace.
 * //from   w ww.ja v  a 2s. c  o m
 * @param n The parent in which to search for children
 * @return The first child Element of n, or null if none
 */
public static Element getFirstChildElement(Node n) {
    Node child = n.getFirstChild();
    while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
        child = child.getNextSibling();
    }

    if (child != null) {
        return (Element) child;
    } else {
        return null;
    }
}

From source file:Main.java

private static Node getNextTypedNode(Node node, short nodeType) {
    node = node.getNextSibling();

    while (node != null && node.getNodeType() != nodeType) {
        node = node.getNextSibling();//from  ww w  .  ja v a  2s.c  o  m
    }

    return node;
}

From source file:Main.java

public static Element findChildElement(Element parent, String name) {
    if (parent == null) {
        return null;
    }//w  w w. j  a v a  2 s .  c o m
    org.w3c.dom.Node ret = parent.getFirstChild();
    while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name))) {
        ret = ret.getNextSibling();
    }
    return (Element) ret;
}

From source file:Main.java

/**
 * Remove <tt>xml:base</tt> attributes from the given node and all of its
 * descendants. According to section 1.3.4 of the DOM Level 3 Core
 * specification, when expanding an external entity reference, DOM parser
 * will add <tt>xml:base</tt> attributes to all elements appearing in the
 * external entity. These attributes don't appear in the reference output
 * files provided in the test suite (see
 * {@link XMLConformanceTest#getOutput()}). This method can be used to
 * remove <tt>xml:base</tt> attributes in DOM documents.
 * /*from   w w  w .  ja  va2  s  .c  o  m*/
 * @param node
 *            the node to process
 */
public static void removeXmlBaseAttributes(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        element.removeAttribute("xml:base");
    }
    Node child = node.getFirstChild();
    while (child != null) {
        removeXmlBaseAttributes(child);
        child = child.getNextSibling();
    }
}

From source file:Main.java

/**
 * get first "real" element (ie not the document-rootelement, but the next one
 */// w  w  w .  j a v  a 2s .c  o m
public static Element getFirstElement(Document document) {
    Element workelement = null;

    if (document.getDocumentElement() != null) {
        org.w3c.dom.Node tmp = document.getDocumentElement().getFirstChild();

        while (tmp != null) {
            tmp = tmp.getNextSibling();

            if (tmp.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                workelement = (Element) tmp;

                break;
            }
        }
    }

    return workelement;
}