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

public static void createFile(String file_path) {

    try {//from   w  w w  . j  a  v a2 s.  c o m
        DocumentBuilderFactory doc_Factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder doc_builder = doc_Factory.newDocumentBuilder();

        // root elements
        Document doc = doc_builder.newDocument();
        Element root_element = doc.createElement("Log");
        doc.appendChild(root_element);

        saveFile(file_path, doc);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static Element createRootElement(Document rootDoc, String rootTagName) {
    if (rootDoc.getDocumentElement() == null) {
        Element root = rootDoc.createElement(rootTagName);
        rootDoc.appendChild(root);
        return root;
    }//w ww  . j a  v  a2 s. co m
    return rootDoc.getDocumentElement();
}

From source file:Main.java

public static String getNodeTextByXPath(Element eoEl, String xpath) {
    try {/*from w w w .  j a  v  a 2s  . c o m*/
        Document doc = newDocument();
        Element importedEl = (Element) doc.importNode(eoEl.cloneNode(true), true);
        doc.appendChild(importedEl);

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpathEn = factory.newXPath();
        return xpathEn.evaluate(xpath, doc);
    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static String elementToString(final Node node) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final Document document = factory.newDocumentBuilder().newDocument();
    final Node importedNode = document.importNode(node, true);
    document.appendChild(importedNode);
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(document), new StreamResult(bos));
    return bos.toString("UTF-8");
}

From source file:Main.java

public static Node addTag(Document doc, String index, String value) {
    org.w3c.dom.Element tag = doc.createElement(index);
    tag.appendChild(doc.createTextNode(value));
    return doc.appendChild(tag);
}

From source file:Main.java

public static Document createXMLResult(String rootName, Map<String, String> map) {

    try {/*from w w  w.ja v  a2 s .c  o m*/
        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        docBuilder = dbfac.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element root = doc.createElement(rootName);
        doc.appendChild(root);
        for (String elementName : map.keySet()) {
            Element child = doc.createElement(elementName);
            Text text = doc.createTextNode(map.get(elementName));
            child.appendChild(text);
            root.appendChild(child);
        }

        return doc;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

public static Document CreateDom() {
    Document newdoc = getDocumentBuilder().newDocument();
    //newdoc..PreserveWhitespace = true;
    Element childnode = newdoc.createElement("root");
    newdoc.appendChild(childnode);
    return newdoc;
}

From source file:Main.java

public static void createXMLDocument(File file, String rootName) {
    if (!file.exists()) {
        try {//from w  w  w.  j  a v a 2  s. c o m
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.newDocument();
            Element root = document.createElement(rootName);
            document.appendChild(root);
            saveXMLDocument(document, file);
        } catch (Exception ex) {
        }
    }
}

From source file:Main.java

public static Document addXMLItems(File xmlfile, String tag) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc;
    if (xmlfile.length() == 0) {
        doc = builder.newDocument();/*from   w  w w . ja va 2 s.  c o  m*/
        Element rootElement = doc.createElement(tag + "s");
        doc.appendChild(rootElement);
    } else {
        doc = builder.parse(xmlfile);
    }
    return doc;
}

From source file:Main.java

/**
 * Copies a document's content to another document (i.e. the root node is
 * adopted by the new document and inserted there). The new document should
 * be empty./*from ww  w  .  j a v a  2 s  .  c o  m*/
 *
 * @param oldDocument
 * @param newDocument
 *
 * @return the new containing document
 */
public static Document copyDocument(Document oldDocument, Document newDocument) {

    Node root = oldDocument.getDocumentElement();

    Node adoptedRoot = newDocument.adoptNode(root);

    newDocument.appendChild(adoptedRoot);

    return newDocument;
}