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 document that has a deep copy of the element passed in as its document element
 * @param element/* w ww . j  a va 2s. com*/
 * @return the new document
 */
public static Document createDocumentFrom(Element element) {
    Document document = createEmptyDocument();
    Node clonedNode = element.cloneNode(true);
    clonedNode = document.adoptNode(clonedNode);
    document.appendChild(clonedNode);
    return document;
}

From source file:Main.java

public static void hashMapToXML222(String xmlFile, String xpath, HashMap hashmap) {
    try {/*from w  w  w .  ja v  a  2s . c  om*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();

        Element rootNode = document.createElement(xpath);
        document.appendChild(rootNode);

        Set set = hashmap.entrySet();
        Iterator i = set.iterator();

        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            Element em = document.createElement(me.getKey().toString());
            em.appendChild(document.createTextNode(me.getValue().toString()));
            rootNode.appendChild(em);
            // System.out.println("write " +
            // me.getKey().toString() + "="
            // + me.getValue().toString());
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        FileOutputStream fo = new FileOutputStream(xmlFile);
        StreamResult result = new StreamResult(fo);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Adds an element to a document in memory
 * //from w ww  .  ja va2  s. c om
 * @param doc
 *            Document where element will be added
 * @param elementName
 *            The name of the element
 * 
 * @return org.w3c.dom.Element
 */
public static Element addElementToDocument(Document doc, String elementName) {
    // Creates the element and adds it to the document
    Element element = doc.createElement(elementName);
    doc.appendChild(element);

    return element;
}

From source file:Main.java

public static <K, V> String mapToXml(Map<K, V> map, String rootName, String childName, String keyName,
        String valueName) throws TransformerException, ParserConfigurationException {
    Document document = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().newDocument();

    Element root = document.createElement(rootName);
    document.appendChild(root);

    for (Map.Entry<K, V> errorCodeReport : map.entrySet()) {
        Element s = document.createElement(childName);
        root.appendChild(s);/*  www. ja v  a  2  s .c  o  m*/

        s.setAttribute(keyName, errorCodeReport.getKey().toString());
        s.setAttribute(valueName, errorCodeReport.getValue().toString());
    }

    return getStringFromDocument(document);
}

From source file:de.betterform.cache.CacheManager.java

/**
 *
 * @param file cache key/*from   ww  w  . j  av a  2 s  .  c o  m*/
 * @return document cache value 
 * @throws XFormsException
 */
public static Document getDocument(File file) throws XFormsException {
    Element elem = getElementFromFileCache(file);
    if (elem == null) {
        return null;
    }

    Document cachedDoc = (Document) elem.getObjectValue();
    org.w3c.dom.Element elment = cachedDoc.getDocumentElement();
    Document document = DOMResource.newDocument();
    //document.appendChild(document.adoptNode(elment.cloneNode(true)));
    document.appendChild(document.importNode(elment, true));
    return document;
}

From source file:Main.java

public static Document convertToDocument(Node node) throws ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);//from   ww  w.j  a  va 2 s .c  o m
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document newDocument = builder.newDocument();
    newDocument.setXmlStandalone(true);
    Node importedNode = newDocument.importNode(node, true);
    newDocument.appendChild(importedNode);
    return newDocument;
}

From source file:Main.java

private static Document documentFromSoapBody(SOAPBodyElement element,
        HashMap<String, String> namespaceDeclarations) throws ParserConfigurationException {
    Document document = dbf.newDocumentBuilder().newDocument();
    Node node = document.importNode(element, true);
    document.appendChild(node);

    for (String prefix : namespaceDeclarations.keySet()) {
        String uri = namespaceDeclarations.get(prefix);
        if (node.lookupNamespaceURI(prefix) == null) {
            document.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + prefix,
                    uri);// www.  j  a  v a 2  s. c om
        }
    }
    return document;
}

From source file:cz.muni.fi.mir.mathmlunificator.utils.DOMBuilder.java

/**
 * Create new W3C DOM document with only child node created as the clone of
 * the given node (detached from the DOM of the source node).
 *
 * @param nodeToClone Node to clone as the only child node of the new
 * document./*  ww  w.  j a v a  2 s . com*/
 * @param deep If true, recursively clone the subtree under the given node;
 * if false, clone only the node itself (and its attributes, if it is an
 * Element).
 * @return New W3C DOM document with the only child node created as the
 * clone of the given node (detached from the DOM of the source node).
 */
public static Document createNewDocWithNodeClone(Node nodeToClone, boolean deep) {
    Document newDoc = DOMBuilder.createEmptyDoc();
    newDoc.appendChild(newDoc.importNode(nodeToClone, deep));
    return newDoc;
}

From source file:Main.java

/**
 * Create an root element with the specified name
 *
 * @param doc/* ww w  .  j  a  va2 s.com*/
 * @param rootTagName
 * @return
 */
public static Element createRootElement(Document doc, String rootTagName) {
    if (doc.getDocumentElement() == null) {
        Element root = doc.createElement(rootTagName);
        doc.appendChild(root);
        return root;
    }
    return doc.getDocumentElement();
}

From source file:Main.java

public static Element createRoot(Document document, String title) {
    Element node = document.createElement(title);
    node.setAttribute("xmlns", "http://www.supermap.com.cn/desktop");
    node.setAttribute("version", "9.0.x");
    document.appendChild(node);
    return node;/* w w  w  .  j a va 2  s  .  com*/
}