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:MainClass.java

public static void main(String[] args)
        throws IOException, ParserConfigurationException, org.xml.sax.SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(true);/*from   ww w. j  av  a  2 s  .com*/
    factory.setCoalescing(true); // Convert CDATA to Text nodes
    factory.setNamespaceAware(false); // No namespaces: this is default
    factory.setValidating(false); // Don't validate DTD: also default

    DocumentBuilder parser = factory.newDocumentBuilder();

    Document document = parser.parse(new File(args[0]));

    NodeList sections = document.getElementsByTagName("sect1");
    int numSections = sections.getLength();
    for (int i = 0; i < numSections; i++) {
        Element section = (Element) sections.item(i); // A <sect1>

        Node title = section.getFirstChild();
        while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
            title = title.getNextSibling();

        if (title != null)
            System.out.println(title.getFirstChild().getNodeValue());
    }
}

From source file:Main.java

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

    factory.setExpandEntityReferences(false);

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

    Document doc2 = factory.newDocumentBuilder().parse(new File("infilename2.xml"));

    // Make a copy of the element subtree suitable for inserting into doc2
    Node node = doc2.importNode(element, true);

    // Get the parent
    Node parent = node.getParentNode();

    // Get children
    NodeList children = node.getChildNodes();

    // Get first child; null if no children
    Node child = node.getFirstChild();

    // Get last child; null if no children
    child = node.getLastChild();

    // Get next sibling; null if node is last child
    Node sibling = node.getNextSibling();

    // Get previous sibling; null if node is first child
    sibling = node.getPreviousSibling();

    // Get first sibling
    sibling = node.getParentNode().getFirstChild();

    // Get last sibling
    sibling = node.getParentNode().getLastChild();

}

From source file:XMLInfo.java

public static void main(String args[]) {
    try {/*from   w w w.  jav a2s  . c o m*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("xmlFileName.xml");
        Node root = document.getDocumentElement();
        System.out.print("Here is the document's root node:");
        System.out.println(" " + root.getNodeName());
        System.out.println("Here are its child elements: ");
        NodeList childNodes = root.getChildNodes();
        Node currentNode;

        for (int i = 0; i < childNodes.getLength(); i++) {
            currentNode = childNodes.item(i);
            System.out.println(currentNode.getNodeName());
        }

        // get first child of root element
        currentNode = root.getFirstChild();

        System.out.print("The first child of root node is: ");
        System.out.println(currentNode.getNodeName());

        // get next sibling of first child
        System.out.print("whose next sibling is: ");
        currentNode = currentNode.getNextSibling();
        System.out.println(currentNode.getNodeName());

        // print value of next sibling of first child
        System.out.println("value of " + currentNode.getNodeName() + " element is: "
                + currentNode.getFirstChild().getNodeValue());

        // print name of parent of next sibling of first child
        System.out.print("Parent node of " + currentNode.getNodeName() + " is: "
                + currentNode.getParentNode().getNodeName());
    }
    // handle exception creating DocumentBuilder
    catch (ParserConfigurationException parserError) {
        System.err.println("Parser Configuration Error");
        parserError.printStackTrace();
    }

    // handle exception reading data from file
    catch (IOException fileException) {
        System.err.println("File IO Error");
        fileException.printStackTrace();
    }

    // handle exception parsing XML document
    catch (SAXException parseException) {
        System.err.println("Error Parsing Document");
        parseException.printStackTrace();
    }
}

From source file:Main.java

public static Element getNextSiblingElement(Node node) {
    Node n = node.getNextSibling();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();//ww  w .j av  a  2 s  .c o  m
    }
    return (Element) n;
}

From source file:Main.java

public static Element findNextElement(Node ret, String name) {
    ret = ret.getNextSibling();
    while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name))) {
        ret = ret.getNextSibling();//from  www . j a va  2  s.  c om
    }
    return (Element) ret;
}

From source file:Main.java

public static Element getNextSiblingElementNode(Node node) {
    Node nextNode = node.getNextSibling();
    while (nextNode.getNodeType() != Node.ELEMENT_NODE) {
        nextNode = nextNode.getNextSibling();
    }// ww  w  .  j av  a2  s  . c  o m

    return (Element) nextNode;
}

From source file:Main.java

public static Node getNextElementSibling(Node node) {
    node = node.getNextSibling();
    while (node != null && node.getNodeType() != Node.ELEMENT_NODE) {
        node = node.getNextSibling();/* w  w  w .j  a v a 2 s.  c o m*/
    }

    return node;
}

From source file:Main.java

/**
 * Gets the next comment./*from w ww . j  a  va 2  s  .co  m*/
 * 
 * @param element
 *            the element
 * @return the next comment
 */
public static String getNextComment(Node element) {
    while (element.getNextSibling() != null) {
        Node prev = element.getNextSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getNextComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

public static Element nextSiblingElement(Node node) {
    for (Node tempNode = node.getNextSibling(); tempNode != null; tempNode = tempNode.getNextSibling()) {
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) tempNode;
        }//  w w w .  j ava  2  s  .  c  om
    }
    return null;
}

From source file:Main.java

/**
 * Get the sibling in next context/*w w w.j av  a  2s .  co  m*/
 * 
 * @param current
 * @return the sibling
 */
public static Node getSibling(Node current) {
    Node sibling = current.getNextSibling();
    Node tmpParent = current;
    while (tmpParent != null && sibling == null) {
        tmpParent = tmpParent.getParentNode();
        if (tmpParent != null) {
            sibling = tmpParent.getNextSibling();
        }
    }
    return sibling;
}