Example usage for org.w3c.dom Element getLastChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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);//www .j  av a2 s.c o m
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    element = doc.createElement("root");
    doc.appendChild(element);

    element.insertBefore(doc.createTextNode("C"), element.getLastChild());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element element = doc.createElement("root");
    doc.appendChild(element);// w ww .j a  v  a 2 s. c om
    Comment comment = doc.createComment("This is a comment");
    doc.insertBefore(comment, element);
    Element itemElement = doc.createElement("item");
    element.appendChild(itemElement);
    itemElement.setAttribute("myattr", "attr>value");
    itemElement.insertBefore(doc.createTextNode("te<xt"), itemElement.getLastChild());
    prettyPrint(doc);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder loader = factory.newDocumentBuilder();
    Document document = loader.parse("sample.xml");

    Element order = document.getDocumentElement();

    DocumentRange ranges = (DocumentRange) document;

    Range range = ranges.createRange();/*from  w  ww.j  a v  a2  s. com*/
    range.setStartBefore(order.getFirstChild());
    range.setEndAfter(order.getLastChild());

    range.deleteContents();
    range.detach();

}

From source file:Main.java

public static void addComment(Document doc) {
    Element root = doc.getDocumentElement();
    Element folks = (Element) root.getLastChild();
    Comment comment = doc.createComment("Text of the comment");
    root.insertBefore(comment, folks);/*  ww w .ja  va2 s . co m*/
}

From source file:Main.java

public static void addProcessingInstruction(Document doc) {
    Element root = doc.getDocumentElement();
    Element folks = (Element) root.getLastChild();
    ProcessingInstruction pi;//from   www .  ja v a 2 s  .c  o  m
    pi = (ProcessingInstruction) doc.createProcessingInstruction("validate", "phone=\"lookup\"");
    root.insertBefore(pi, folks);
}

From source file:Main.java

public static void addCDATA(Document doc) {
    Element root = doc.getDocumentElement();
    Element place = (Element) root.getFirstChild();
    Element directions = (Element) place.getLastChild();
    String dirtext = "cdData.";
    CDATASection dirdata = doc.createCDATASection(dirtext);
    directions.replaceChild(dirdata, directions.getFirstChild());
}

From source file:Main.java

public static Element getLastChild(Element e) {
    if (e == null)
        return null;
    Node n = e.getLastChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getPreviousSibling();//from w  w  w .  j a v  a2 s .  com
    return (Element) n;
}

From source file:Main.java

public static void modifyTextByReplacement(Document doc) {
    Element root = doc.getDocumentElement();
    Element place = (Element) root.getFirstChild();
    Text name = (Text) place.getFirstChild().getFirstChild();
    Text directions = (Text) place.getLastChild().getFirstChild();

    name.setData("newName");
    directions.setData("newDirection");
}

From source file:Main.java

public static void modifyingTextbyCuttingandPasting(Document doc) {
    Element root = doc.getDocumentElement();
    Element place = (Element) root.getFirstChild();
    Text name = (Text) place.getFirstChild().getFirstChild();
    Text directions = (Text) place.getLastChild().getFirstChild();

    //    name.deleteData(offset,count);
    name.deleteData(2, 3);//from  w ww  .j  a  va2 s. com

    //String bridge = directions.substringData(offset,count);
    String bridge = directions.substringData(2, 3);
    name.appendData(bridge);
}

From source file:Main.java

public static void editTextbyInsertionandReplacement(Document doc) {
    Element root = doc.getDocumentElement();
    Element place = (Element) root.getFirstChild();
    Text name = (Text) place.getFirstChild().getFirstChild();
    Text directions = (Text) place.getLastChild().getFirstChild();

    // Inserting the word "black"
    //    name.insertData(offset," black");
    name.insertData(3, " black");

    // Replace "left" with "right"
    //directions.replaceData(offset,count,"right");
    directions.replaceData(3, 3, "right");
}