Example usage for org.w3c.dom Node getLastChild

List of usage examples for org.w3c.dom Node getLastChild

Introduction

In this page you can find the example usage for org.w3c.dom Node getLastChild.

Prototype

public Node getLastChild();

Source Link

Document

The last child of 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  va  2 s  .  c o 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:Main.java

public static Element lastChildElement(Node node) {
    for (Node tempNode = node.getLastChild(); tempNode != null; tempNode = tempNode.getPreviousSibling()) {
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) tempNode;
        }/*from   ww w . j av a2s.  c o m*/
    }
    return null;
}

From source file:Main.java

/**
 * Remove all children from the specified node
 *//* w  w w  . j  a  va  2  s.c  om*/
public static void removeAllChildren(Node node) {
    while (node.hasChildNodes())
        node.removeChild(node.getLastChild());
}

From source file:Main.java

/** Finds and returns the last child element node. 
 *  Overload previous method for non-Xerces node impl.
 *//*from   w  w  w . j  a va  2s .  co  m*/
public static Element getLastChildElement(Node parent) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(Node parent, String elemNames[]) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                if (child.getNodeName().equals(elemNames[i])) {
                    return (Element) child;
                }/*from  w w  w .  j a v  a  2 s  .c  o  m*/
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/** Finds and returns the last child node with the given qualified name. */
public static Element getLastChildElementNS(Node parent, String[][] elemNames) {

    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            for (int i = 0; i < elemNames.length; i++) {
                String uri = child.getNamespaceURI();
                if (uri != null && uri.equals(elemNames[i][0])
                        && child.getLocalName().equals(elemNames[i][1])) {
                    return (Element) child;
                }/*from   w  w  w .  j  av a2 s .  c o m*/
            }
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Finds and returns the last child node with the given name and
 * attribute name, value pair./*  w w w  .  j a  v  a 2  s .  co  m*/
 */
public static Element getLastChildElement(Node parent, String elemName, String attrName, String attrValue) {

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

    // not found
    return null;

}

From source file:Main.java

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

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }//w w  w .  j av a 2 s .  com
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/** Finds and returns the last child node with the given name. */
public static Element getLastChildElement(Node parent, String elemName) {

    if (parent == null)
        return null;
    // search for node
    Node child = parent.getLastChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (child.getNodeName().equals(elemName)) {
                return (Element) child;
            }//from   www .ja v a 2  s  . c  o  m
        }
        child = child.getPreviousSibling();
    }

    // not found
    return null;

}

From source file:Main.java

/**
 * Get the previous node in a DFS preorder traversal
 *
 * @param node the current node//from  w w  w . j  a  va  2 s. c o  m
 * @return the previous node in the preorder traversal
 */
public static Node getPrevious(Node node) {
    Node previous;

    if (node == null)
        return null;

    if ((previous = node.getPreviousSibling()) != null) {
        for (; previous.getLastChild() != null; previous = previous.getLastChild()) {
        }

        return previous;
    }

    return node.getParentNode();
}