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

/**
 * Returns a single element having a given tag
 *///from   w  w  w .j  a  v  a 2 s  . c  om
public static Element getSingleElement(Element element, String tagName) {
    Node node = element.getFirstChild();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().compareTo(tagName) == 0)
            return (Element) node;

        node = node.getNextSibling();
    }

    return null;
}

From source file:Main.java

/**
 * @param sibling//w w w.  ja va2 s .c o m
 * @param uri
 * @param nodeName
 * @param number
 * @return nodes with the constrain
 */
public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) {
    Node n = selectNode(sibling, uri, nodeName, number);
    if (n == null) {
        return null;
    }
    n = n.getFirstChild();
    while (n != null && n.getNodeType() != Node.TEXT_NODE) {
        n = n.getNextSibling();
    }
    return (Text) n;
}

From source file:Main.java

public static void stripDuplicateAttributes(Node node, Node parent) {

    // The output depends on the type of the node
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE: {
        Document doc = (Document) node;
        Node child = doc.getFirstChild();
        while (child != null) {
            stripDuplicateAttributes(child, node);
            child = child.getNextSibling();
        }/*from  ww w.  ja  va2s .  c om*/
        break;
    }

    case Node.ELEMENT_NODE: {
        Element elt = (Element) node;
        NamedNodeMap attrs = elt.getAttributes();

        ArrayList nodesToRemove = new ArrayList();
        int nodesToRemoveNum = 0;

        for (int i = 0; i < attrs.getLength(); i++) {
            final Node a = attrs.item(i);

            for (int j = 0; j < attrs.getLength(); j++) {
                final Node b = attrs.item(j);

                //if there are two attributes with same name
                if (i != j && (a.getNodeName().equals(b.getNodeName()))) {
                    nodesToRemove.add(b);
                    nodesToRemoveNum++;
                }
            }
        }

        for (int i = 0; i < nodesToRemoveNum; i++) {
            Attr nodeToDelete = (Attr) nodesToRemove.get(i);
            Element nodeToDeleteParent = (Element) node; // nodeToDelete.getParentNode();
            nodeToDeleteParent.removeAttributeNode(nodeToDelete);
        }

        nodesToRemove.clear();

        Node child = elt.getFirstChild();
        while (child != null) {
            stripDuplicateAttributes(child, node);
            child = child.getNextSibling();
        }

        break;
    }

    default:
        //do nothing
        break;
    }
}

From source file:Main.java

/** Finds and returns the first child element node. */
public static Element getFirstChildElement(Node parent) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }/*from   ww  w.j  a  v  a 2 s . c o  m*/
        child = child.getNextSibling();
    }

    // not found
    return null;

}

From source file:Main.java

public static String cdataValue(Element element) {
    if (element == null)
        return null;
    // get the first element with the given name
    Node node = element.getFirstChild();

    if (node != null) {
        do {/*from ww  w  . j ava  2  s . co m*/
            if (node.getNodeType() == Node.CDATA_SECTION_NODE) {
                return node.getNodeValue();
            }
        } while ((node = node.getNextSibling()) != null);
    }
    return null;
}

From source file:Main.java

public static void collectXpathContainText(Node node, String textContent, List<String> holder) {
    if (textContent.equals(node.getTextContent())) {
        String xpath = getXPath(node);
        if (!holder.contains(xpath)) {
            holder.add(xpath);/*from w  w w.  j  a  v a  2  s .co  m*/
        }
    }

    if (node.hasChildNodes()) {
        Node child = node.getFirstChild();
        while (child != null) {
            collectXpathContainText(child, textContent, holder);
            child = child.getNextSibling();
        }
    }
}

From source file:Main.java

/**
 * Get the next node in a depth first preorder traversal.
 *
 * <ul>// w w  w.  ja  va  2s.  c om
 * <li>If the node has a child, return the child
 * <li>Else if the node has a following sibling, return that sibling
 * <li>Else if the node has a following uncle, return that uncle
 * </ul>
 *
 * @param node the current node
 * @return the next node in the preorder traversal
 */
public static Node getNext(Node node) {
    if (node == null)
        return null;

    if (node.getFirstChild() != null)
        return node.getFirstChild();

    for (; node != null; node = node.getParentNode()) {
        if (node.getNextSibling() != null)
            return node.getNextSibling();
    }

    return null;
}

From source file:Main.java

public static Element getFirstChildElementByLocalName(Element parentElem, String localName) {
    Node child = parentElem.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && getLocalName(child).equals(localName)) {
            return (Element) child;
        }//w w w.j  av a 2  s  . c om
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Gets the first (direct) child Element.
 * //ww w  .j  a va2  s . c om
 * @param parent the parent element below which to search the child
 * @return the first child element, or null otherwise
 */
static public Element getFirstChild(Node parent) { // Child Element suchen
    if (parent == null)
        return null;
    Node node = parent.getFirstChild();
    while (node != null) { // Find all Element nodes
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node; // found
        node = node.getNextSibling();
    }
    return null; // not found!
}

From source file:Main.java

public static List<Element> getNamedChildElements(Element parent, 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  . ja v  a2 s .  com
            child = child.getNextSibling();
        }
    }
    return elements;
}