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

/**
 * Creates a new {@link Document}./*from  ww w.  ja va 2 s.co m*/
 *
 * @param rootElementName The name of the root element.
 * @param xmlns The namespace of the document.
 * @return Returns said {@link Document}.
 */
public static Document createDocument(String rootElementName, String xmlns) {
    final Document doc = createDocument();
    final Element root = doc.createElement(rootElementName);
    if (xmlns != null)
        root.setAttribute("xmlns", xmlns);
    doc.appendChild(root);
    return doc;
}

From source file:Main.java

/**
 * Adds an element to the specified document.
 * @param doc the document.//  ww w  .  ja  v a  2  s  . c om
 * @param parent the parent node.
 * @param name the element name.
 * @return the element.
 */
public static Element addElement(final Document doc, final Node parent, final String name) {
    Element element = doc.createElement(name);
    if (parent != null) {
        parent.appendChild(element);
    } else {
        doc.appendChild(element);
    }
    return element;
}

From source file:Main.java

public static String transformXmlToString(Document doc, String encoding)
        throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();

    Document document = builder.newDocument();

    Element e = doc.getDocumentElement();
    Node n = document.importNode(e, true);
    document.appendChild(n);

    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();

    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(document);

    StringWriter sw = new StringWriter();

    StreamResult result = new StreamResult(sw);

    transformer.transform(source, result);

    return sw.toString();
}

From source file:Main.java

public static Document startXMLDocument(String _firstTag) {
    DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = null;
    Document doc = null;
    try {//from   ww  w.  j  av a2  s .  c o  m
        docBuilder = dbfac.newDocumentBuilder();
        doc = docBuilder.newDocument();

        Element xmlRoot = doc.createElement(_firstTag);
        doc.appendChild(xmlRoot);

    } catch (ParserConfigurationException e) {
        System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage());
        return null;
    }
    return doc;
}

From source file:Main.java

private static Document pruneDocument(Document doc, int maxElement) throws ParserConfigurationException {
    if (maxElement == -1) {
        return doc;
    }//from  w ww .  j a v  a 2s.c  o m

    Document newDoc = (Document) doc.cloneNode(false);
    Element newRoot = (Element) doc.getDocumentElement().cloneNode(false);
    newDoc.adoptNode(newRoot);
    newDoc.appendChild(newRoot);

    NodeList nl = doc.getDocumentElement().getChildNodes();
    for (int i = 0; i < maxElement && i < nl.getLength(); i++) {
        if (!(nl.item(i) instanceof Element)) {
            maxElement++;
            continue;
        }

        Node item = nl.item(i).cloneNode(true);
        newDoc.adoptNode(item);
        newDoc.getDocumentElement().appendChild(item);
    }

    if (debug)
        System.out.println("Creating document of " + newDoc.getDocumentElement().getChildNodes().getLength());
    return newDoc;
}

From source file:Main.java

/**
 * Clones a document object.//from   w  w w  .  j  a va 2s  .  com
 *
 * @param doc The document to be cloned.
 * @return The new document object that contains the same data as the original document.
 * @throws TransformerException Thrown if the document can't be
 */
public static Document cloneDocument(final Document doc) throws TransformerException {
    final Node rootNode = doc.getDocumentElement();

    // Copy the doctype and xml version type data
    final TransformerFactory tfactory = TransformerFactory.newInstance();
    final Transformer tx = tfactory.newTransformer();
    final DOMSource source = new DOMSource(doc);
    final DOMResult result = new DOMResult();
    tx.transform(source, result);

    // Copy the actual content into the new document
    final Document copy = (Document) result.getNode();
    copy.removeChild(copy.getDocumentElement());
    final Node copyRootNode = copy.importNode(rootNode, true);
    copy.appendChild(copyRootNode);

    return copy;
}

From source file:io.selendroid.server.model.internal.JsonXmlUtil.java

public static Document buildXmlDocument(JSONObject tree) {
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {/* w  w  w.  j av  a2s .co  m*/
        builder = builderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        SelendroidLogger.error("Failed to create documentBuilder", e);
        throw new RuntimeException(e);
    }
    Document document = builder.newDocument();

    // document.setXMLEncoding("UTF-8");
    Element root = document.createElement("views");
    document.appendChild(root);
    buildXmlNode(tree, root, document);
    return document;
}

From source file:Main.java

public static Document addRoot(Document dataDoc, String elementName) {
    Element oldRoot = dataDoc.getDocumentElement();
    Element newRoot = dataDoc.createElement(elementName);
    dataDoc.removeChild(oldRoot);//from   w  w w .  j av a2s. co  m
    newRoot.appendChild(oldRoot);
    dataDoc.appendChild(newRoot);
    return dataDoc;
}

From source file:Main.java

/**
 * createXMLDoc(String) Given a name for the root element will create and
 * return a org.w3c.dom.Document with the root node specified. The Document
 * will be based on the Document implementation as specified in the system
 * properties file XML_DOCUMENT_CLASS//from w ww .j  av  a 2  s.  c  o  m
 * 
 * @param String
 *            rootName
 * @return Document
 * @author Peter Manta (after blatant copying of Jeff)
 */
public static org.w3c.dom.Document createXMLDoc(String rootName) {
    Class docClass = null;
    Document doc = null;
    Element root = null;
    try {
        docClass = Class.forName(XML_DOCUMENT_IMPL);
        doc = (Document) docClass.newInstance();
        root = doc.createElement(rootName);
        doc.appendChild(root);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    }
    return doc;
}

From source file:Main.java

public static void writeXMLObject(String path, String name, String tag, String model, double ry, String version,
        double x, double y, double z) {
    try {//from  w  w w .  j  av  a2  s  . c  o  m
        id++;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // Root elements
        Document doc = docBuilder.newDocument();
        doc.setXmlStandalone(true);
        Element rootElement = doc.createElement(tag);
        doc.appendChild(rootElement);

        rootElement.setAttributeNode(addAttribut(doc, "action", "void"));
        rootElement.setAttributeNode(addAttribut(doc, "actionDist", "10.0"));
        rootElement.setAttributeNode(addAttribut(doc, "collidable", "true"));
        rootElement.setAttributeNode(addAttribut(doc, "id", id + ""));
        rootElement.setAttributeNode(addAttribut(doc, "model", model));
        rootElement.setAttributeNode(addAttribut(doc, "pCameraFacing", "true"));
        rootElement.setAttributeNode(addAttribut(doc, "pControlFlow", "false"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitH", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitInnerRadius", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitOutterRadius", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitType", "0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEmitW", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndA", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndB", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndMass", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndR", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndSize", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pEndV", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pFactoryNumber", "500"));
        rootElement.setAttributeNode(addAttribut(doc, "pInitialVelocity", "0.0030"));
        rootElement.setAttributeNode(addAttribut(doc, "pMaxAngle", "10.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMaxLife", "2000.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMinAngle", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pMinLife", "1000.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSecVar", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pParticulesPerSeconds", "100"));
        rootElement.setAttributeNode(addAttribut(doc, "pSpeed", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartA", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartB", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartMass", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartR", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartSize", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "pStartV", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "rx", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "ry", ry + ""));
        rootElement.setAttributeNode(addAttribut(doc, "rz", "0.0"));
        rootElement.setAttributeNode(addAttribut(doc, "s", "1.0"));
        rootElement.setAttributeNode(addAttribut(doc, "type", "basic"));
        rootElement.setAttributeNode(addAttribut(doc, "versionCode", version));
        rootElement.setAttributeNode(addAttribut(doc, "x", x + ""));
        rootElement.setAttributeNode(addAttribut(doc, "y", y + ""));
        rootElement.setAttributeNode(addAttribut(doc, "z", z + ""));

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(path + "\\" + name + id + ".xml");
        transformer.transform(source, result);
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    }
}