Example usage for org.w3c.dom Element getNextSibling

List of usage examples for org.w3c.dom Element getNextSibling

Introduction

In this page you can find the example usage for org.w3c.dom Element getNextSibling.

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from w  w  w  . j  a v  a2 s  . c  o  m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    NodeList list = doc.getElementsByTagName("entry");
    Element element = (Element) list.item(0);

    Element dup = (Element) element.cloneNode(true);

    element.getParentNode().insertBefore(dup, element.getNextSibling());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from ww  w .j  a v  a 2s.c om*/

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        Comment comment = doc.createComment("index=" + i);

        element.getParentNode().insertBefore(comment, element.getNextSibling());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from   w  w  w . j  av  a2  s.  c  o m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    // Find all elements with the name "entry" and append a comment
    NodeList list = doc.getElementsByTagName("entry");
    for (int i = 0; i < list.getLength(); i++) {
        Element element = (Element) list.item(i);
        Comment comment = doc.createComment("index=" + i);
        // Add the comment after this element
        element.getParentNode().insertBefore(comment, element.getNextSibling());
    }
}

From source file:ListMoviesXML.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);/*  www  . j a  v a  2 s  .  c o m*/

    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(new InputSource("y.xml"));
    Element root = doc.getDocumentElement();

    Element movieElement = (Element) root.getFirstChild();
    Movie m;
    while (movieElement != null) {
        m = getMovie(movieElement);
        String msg = Integer.toString(m.year);
        msg += ": " + m.title;
        msg += " (" + m.price + ")";
        System.out.println(msg);
        movieElement = (Element) movieElement.getNextSibling();
    }
}

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 w  w  .  j  a  v  a  2 s. co  m
    }
    return null;
}

From source file:Main.java

public static Element getNextSibling(Element e) {
    Node n = e.getNextSibling();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getNextSibling();//from   www .  ja  v a2s  .c o  m
    return (Element) n;
}

From source file:Main.java

public static Element findNextSiblingElement(Element current) {
    org.w3c.dom.Node ret = current.getNextSibling();
    while (ret != null) {
        if (ret instanceof Element) {
            return (Element) ret;
        }/* w  ww .  j  a  va2s .co m*/
        ret = ret.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static void insertSiblingAfter(Element newElement, Element sibling) {
    Node nextSibling = sibling.getNextSibling();
    sibling.getParentNode().insertBefore(newElement, nextSibling);
}

From source file:Main.java

/**
 * Insert the given {@code newElement} just next to the given {@code sibling}, after it.
 *
 * @param newElement/*w  ww  . jav  a2  s.  c o m*/
 * @param sibling
 */
public static void insertSiblingAfter(@Nonnull Element newElement, @Nonnull Element sibling) {
    Node nextSibling = sibling.getNextSibling();
    sibling.getParentNode().insertBefore(newElement, nextSibling);
}

From source file:Main.java

public static Element getNextSiblingElement(Element element) {

    try {/*ww  w  .  ja  va2 s  . com*/
        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;
    }
}