Example usage for org.w3c.dom Document appendChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Document 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

/**
 * Returns a document with a HTML node containing a HEAD and BODY node.
 *//* www .  j  a v  a2 s. c  o m*/
public static Document createHtmlDocument() {
    Document document = createDocument();

    Element root = document.createElement("html");

    document.appendChild(root);

    Element head = document.createElement("head");
    root.appendChild(head);

    Element body = document.createElement("body");
    root.appendChild(body);

    return document;
}

From source file:Main.java

private static Element constructDocument() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    Element rootElement = doc.createElement("root");
    doc.appendChild(rootElement);
    Element c1Element = doc.createElement("c1");
    Element c2Element = doc.createElement("c2");
    Element c3Element = doc.createElement("c3");
    Element gc1Element = doc.createElement("gc1_1");
    Element gc2Element = doc.createElement("gc1_2");
    Text textNode = doc.createTextNode("uncle_freddie");
    Element gc3Element = doc.createElement("gc2_1");

    rootElement.appendChild(c1Element);
    rootElement.appendChild(c2Element);
    rootElement.appendChild(c3Element);

    c1Element.appendChild(gc1Element);
    c1Element.appendChild(gc2Element);
    c2Element.appendChild(gc3Element);

    gc3Element.appendChild(textNode);

    return rootElement;
}

From source file:Main.java

public static synchronized void createStorage() throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement(MESSAGES);
    doc.appendChild(rootElement);

    Transformer transformer = getTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(STORAGE_LOCATION));
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * Print out the node in XML format//from   w ww  .  j  a  v  a 2s. c om
 * 
 * @param node
 * @param out
 */
public static void printNode(Node node, PrintStream out) {
    Document doc = new DocumentImpl();
    Element topLevel = doc.createElement("xml");
    doc.appendChild(topLevel);
    topLevel.appendChild(doc.importNode(node, true));
    OutputFormat format = new OutputFormat(doc);
    format.setLineWidth(65);
    format.setIndenting(true);
    format.setIndent(2);
    XMLSerializer serializer = new XMLSerializer(out, format);
    try {
        serializer.serialize(doc);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }

}

From source file:Main.java

public static void buildSkeleton() {
    try {/*w  w  w .ja v  a  2  s .c om*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBldr = dFactory.newDocumentBuilder();

        Document doc = dBldr.newDocument();
        Element rootElement = doc.createElement("hooks");
        doc.appendChild(rootElement);

        write(doc);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Document createDocument(Element element) {
    Document newDocument = getJaxpDocBuilder().newDocument();
    Node node = newDocument.importNode(element, true);
    newDocument.appendChild(node);
    return newDocument;
}

From source file:Main.java

public static Element addDocumentElement(Document doc, String nodeName) {
    Element el = null;/*from  w w w  . j  a  v  a2s.c o m*/

    if (doc != null) {
        el = doc.createElement(nodeName);
        doc.appendChild(el);
    }

    return el;
}

From source file:Main.java

public static Document blankDocument(String paramString) throws Exception {
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    localDocumentBuilderFactory.setIgnoringComments(false);
    localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
    localDocumentBuilderFactory.setValidating(false);
    localDocumentBuilderFactory.setCoalescing(false);
    DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
    Document localDocument = localDocumentBuilder.newDocument();
    Element localElement = localDocument.createElement(paramString);
    localDocument.appendChild(localElement);
    return localDocument;
}

From source file:Main.java

/**
 * Append a <code>fatalError</code> tag to the result xml document.
 * //from  w ww .  ja  va  2 s  .c  o m
 * @param output
 *            the output w3c XML Document object
 */
public static void fatalError(Document output, String note) {
    Element fatalError = output.createElement("fatalError");
    fatalError.setAttribute("note", note);
    output.appendChild(fatalError);
}

From source file:Main.java

public static Document createDocument(String paramString) throws Exception {
    DocumentBuilderFactory localDocumentBuilderFactory = getDocumentBuilderFactoryInstance();
    DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
    Document localDocument = localDocumentBuilder.newDocument();
    Element localElement = localDocument.createElement(paramString);
    localDocument.appendChild(localElement);
    return localDocument;
}