Example usage for org.w3c.dom Element appendChild

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

Introduction

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

Prototype

public Node appendChild(Node newChild) throws DOMException;

Source Link

Document

Adds the node newChild to the end of the list of children of this node.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//  www .  j av  a 2s .  co  m
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    Element root = doc.createElementNS(null, "person"); // Create Root Element
    Element item = doc.createElementNS(null, "name"); // Create element
    item.appendChild(doc.createTextNode("Jeff"));
    root.appendChild(item); // Attach element to Root element
    item = doc.createElementNS(null, "age"); // Create another Element
    item.appendChild(doc.createTextNode("28"));
    root.appendChild(item); // Attach Element to previous element down tree
    item = doc.createElementNS(null, "height");
    item.appendChild(doc.createTextNode("1.80"));
    root.appendChild(item); // Attach another Element - grandaugther
    doc.appendChild(root); // Add Root to Document

    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

    LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer
                                                       // for the DOM
    LSOutput out = domImplLS.createLSOutput();
    StringWriter stringOut = new StringWriter(); // Writer will be a String
    out.setCharacterStream(stringOut);
    ser.write(doc, out); // Serialize the DOM

    System.out.println("STRXML = " + stringOut.toString()); // DOM as a String
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("test.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document oldDoc = builder.parse(is);
    Node oldRoot = oldDoc.getDocumentElement();
    Document newDoc = builder.newDocument();
    Element newRoot = newDoc.createElement("newroot");
    newDoc.appendChild(newRoot);/*w w  w  .  ja  v a  2  s  .c om*/
    newRoot.appendChild(newDoc.importNode(oldRoot, true));

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DOMSource domSource = new DOMSource(newDoc);
    Writer writer = new OutputStreamWriter(out);
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    writer.flush();

    InputStream isNewXML = new ByteArrayInputStream(out.toByteArray());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/* w ww .  j  a  va2 s  .  com*/
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");
    element = doc.createElement("root");
    doc.appendChild(element);

    element.appendChild(doc.createTextNode("D"));

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document xmldoc = impl.createDocument(null, "XMLDoc", null);

    Element root = xmldoc.getDocumentElement();
    Element e0 = xmldoc.createElement("Doc");
    Element e1 = xmldoc.createElement("TITLE");
    Node n1 = xmldoc.createTextNode("Java");
    e1.appendChild(n1);

    Element e2 = xmldoc.createElement("data");
    Node n2 = xmldoc.createTextNode("text node");
    e2.appendChild(n2);/*  w  w w .j  a  v a2  s  .com*/

    e0.appendChild(e1);
    e0.appendChild(e2);
    root.appendChild(e0);

    StreamResult out = new StreamResult("howto.xml");
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document xmldoc = impl.createDocument(null, "TODOs", null);

    Element root = xmldoc.getDocumentElement();
    Element e0 = xmldoc.createElement("TOPIC");
    Element e1 = xmldoc.createElement("TITLE");
    Node n1 = xmldoc.createTextNode("Java");
    e1.appendChild(n1);

    Element e2 = xmldoc.createElement("URL");
    Node n2 = xmldoc.createTextNode("http://www.server.com");
    e2.appendChild(n2);/*from  w  ww  . jav a  2  s .  co  m*/
    e0.appendChild(e1);
    e0.appendChild(e2);
    root.appendChild(e0);

    Node pi = xmldoc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"TODOs.xsl\"");
    xmldoc.insertBefore(pi, root);

    StreamResult out = new StreamResult("howto.xml");
    DOMSource domSource = new DOMSource(xmldoc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(domSource, out);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document doc = impl.createDocument(null, null, null);
    Element e1 = doc.createElement("api");
    doc.appendChild(e1);/*  w  ww  .j  a  v  a2  s. c  om*/
    Element e2 = doc.createElement("java");
    e1.appendChild(e2);

    e2.setAttribute("url", "http://www.domain.com");
    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    System.out.println(sw.toString());
}

From source file:DOMGenerate.java

public static void main(String[] argv) {
    try {//from   w w w .  j  a v a  2s  .co m
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();

        Element root = doc.createElementNS(null, "person"); // Create Root Element
        Element item = doc.createElementNS(null, "name"); // Create element
        item.appendChild(doc.createTextNode("Jeff"));
        root.appendChild(item); // Attach element to Root element
        item = doc.createElementNS(null, "age"); // Create another Element
        item.appendChild(doc.createTextNode("28"));
        root.appendChild(item); // Attach Element to previous element down tree
        item = doc.createElementNS(null, "height");
        item.appendChild(doc.createTextNode("1.80"));
        root.appendChild(item); // Attach another Element - grandaugther
        doc.appendChild(root); // Add Root to Document

        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

        LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer
                                                           // for the DOM
        LSOutput out = domImplLS.createLSOutput();
        StringWriter stringOut = new StringWriter(); // Writer will be a String
        out.setCharacterStream(stringOut);
        ser.write(doc, out); // Serialize the DOM

        System.out.println("STRXML = " + stringOut.toString()); // Spit out the
                                                                // DOM as a String
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document doc = impl.createDocument(null, null, null);
    Element e1 = doc.createElement("api");
    doc.appendChild(e1);/* w  w  w .ja va  2  s  .co  m*/
    Element e2 = doc.createElement("java");
    e1.appendChild(e2);

    e2.setAttribute("url", "http://www.java2s.com");

    //transform the DOM for showing the result in console
    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    System.out.println(sw.toString());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document doc = impl.createDocument(null, null, null);
    Element e1 = doc.createElement("api");
    doc.appendChild(e1);//from w  w  w  .j  a va2  s.  c  om
    Element e2 = doc.createElement("java");
    e1.appendChild(e2);

    e2.setAttribute("url", "http://www.java2s.com");

    // transform the DOM for showing the result in console
    DOMSource domSource = new DOMSource(doc);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    System.out.println(sw.toString());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Document currentstudents = getCurrentstudents();
    Element student = getNewstudent();
    Element ndestudent = (Element) currentstudents.getElementsByTagName("students").item(0);
    Node firstDocImportedNode = currentstudents.importNode(student, true);
    ndestudent.appendChild(firstDocImportedNode);
}