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 addNamespaceMapping(Element importsElement, String dictionaryPrefix, String dictionaryUri) {
    Element importElement = importsElement.getOwnerDocument().createElement("import");
    importElement.setAttribute("uri", dictionaryUri);
    importElement.setAttribute("prefix", dictionaryPrefix);
    importsElement.appendChild(importElement);
}

From source file:Main.java

/**
 * Creates (only if necessary) and returns the element which is at the end of the specified
 * path.//from w  w w . j  av  a2  s.  c om
 * 
 * @param doc
 *            the target document where the specified path should be created
 * @param path
 *            a dot separated string indicating the path to be created
 * @return the component at the end of the newly created path.
 */
public static Element createLastPathComponent(Document doc, String[] path) {
    Element parent = (Element) doc.getFirstChild();
    if (path == null || parent == null || doc == null) {
        throw new IllegalArgumentException("Document parent and path must not be null");
    }

    Element e = parent;
    for (int i = 0; i < path.length; i++) {
        Element newEl = getChildElementByTagName(e, path[i]);
        if (newEl == null) {
            newEl = doc.createElement(path[i]);
            e.appendChild(newEl);
        }
        e = newEl;
    }
    return e;
}

From source file:Main.java

public static void setChildElementText(Element element, String name, String text) {
    Element elm = getFirstChildElement(element, name);
    if (elm == null) {
        elm = element.getOwnerDocument().createElement(name);
        element.appendChild(elm);
    }//from  w w w  .j  a v  a2  s  .  com

    setElementText(elm, text);
}

From source file:Main.java

public static String map2Xml(Map<String, String> content, String root_elem_id, String item_elem_id)
        throws Exception {
    DocumentBuilder b = documentBuilder();
    Document doc = b.newDocument();
    String str = null;//from   w  w w.ja  v a2s  .c  o m
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    Element root = doc.createElement(root_elem_id);
    doc.appendChild(root);

    // Now, add all the children
    for (Entry<String, String> e : content.entrySet()) {
        Element item = doc.createElement(item_elem_id);
        item.setAttribute("id", e.getKey());
        CDATASection data = doc.createCDATASection(e.getValue());
        item.appendChild(data);
        root.appendChild(item);
    }

    try {
        DOMSource ds = new DOMSource(doc);
        StreamResult sr = new StreamResult(out);
        TransformerHandler th = tf.newTransformerHandler();
        th.getTransformer().setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        Properties format = new Properties();
        format.put(OutputKeys.METHOD, "xml");
        //         format.put("{http://xml. customer .org/xslt}indent-amount", "4");
        //         format.put("indent-amount", "4");
        //         format.put(OutputKeys.DOCTYPE_SYSTEM, "myfile.dtd");
        format.put(OutputKeys.ENCODING, "UTF-8");
        format.put(OutputKeys.INDENT, "yes");

        th.getTransformer().setOutputProperties(format);
        th.setResult(sr);
        th.getTransformer().transform(ds, sr);

        str = out.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return str;
}

From source file:Main.java

public static void moveElement(Document fromDoc, Document toDoc, Element root, String elementName) {
    NodeList list = fromDoc.getElementsByTagName(elementName);
    if ((list != null) && (list.getLength() > 0)) {
        Element element = (Element) list.item(0);
        Node node = toDoc.importNode(element, true);
        root.appendChild(node);
        element.getParentNode().removeChild(element);
    }//w  w  w  . ja  v a 2  s .c  om
}

From source file:Main.java

public static void appendStringElement(Element parentElement, String nodeName, String nodeValue) {
    // check for exists
    Element elem = selectSingleElement(parentElement, nodeName);

    if (elem == null) {
        elem = parentElement.getOwnerDocument().createElement(nodeName);
        parentElement.appendChild(elem);
    }/*  ww  w . ja v a  2  s  . c o  m*/

    elem.setTextContent(nodeValue);
}

From source file:Main.java

/**
 * Add the service references to the document.
 *
 * @param document//from   ww w  .  j  av a2s.c  om
 */
// FIXME: This provides trivial information. Necessary?
public static void addServices(Document document) {

    if (document != null) {

        Element root = document.getDocumentElement();

        if (root != null) {

            Element element = document.createElement("services");

            Element service = document.createElement("service");
            service.setAttribute("name", "run");
            service.setAttribute("url", "run");
            element.appendChild(service);

            service = document.createElement("service");
            service.setAttribute("name", "module");
            service.setAttribute("url", "module");
            element.appendChild(service);

            root.appendChild(element);
        }
    }
}

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

private static void buildXmlNode(JSONObject from, Element parent, Document document) {
    if (from == null) {
        return;/*from   w  w  w.  java 2s  .com*/
    }

    Element node = document.createElement(extractTagName(from.optString("type")));
    parent.appendChild(node);

    node.setAttribute("name", from.optString("name"));
    node.setAttribute("label", from.optString("label"));
    node.setAttribute("value", from.optString("value"));
    node.setAttribute("ref", from.optString("ref"));
    node.setAttribute("id", from.optString("id"));
    node.setAttribute("shown", from.optString("shown"));

    JSONObject rect = from.optJSONObject("rect");
    if (rect != null) {
        Element rectNode = document.createElement("rect");
        JSONObject size = rect.optJSONObject("size");
        JSONObject origin = rect.optJSONObject("origin");

        rectNode.setAttribute("x", origin.optString("x"));
        rectNode.setAttribute("y", origin.optString("y"));
        rectNode.setAttribute("height", size.optString("height"));
        rectNode.setAttribute("width", size.optString("width"));

        node.appendChild(rectNode);
    }

    JSONArray array = from.optJSONArray("children");
    if (array != null) {
        for (int i = 0; i < array.length(); i++) {
            JSONObject n = array.optJSONObject(i);
            buildXmlNode(n, node, document);
        }
    }
}

From source file:Main.java

/**
 * Find or create a child element with the given name
 * @param element//  w  w  w  .  ja  v  a2 s  .com
 * @param name
 * @return new or existing element
 */
public static Element findOrCreateChildElement(Document document, Element element, String name) {
    Element[] elements = getChildElementsNamed(element, name);
    Element kid;
    if (elements.length > 0) {
        kid = elements[0];
    } else {
        kid = document.createElement(name);
        element.appendChild(kid);
    }
    return kid;
}

From source file:Main.java

/**
 * Creates a XML document with the given root element and the given {@link NodeList} as content.
 * @param root The root element of the XML document.
 * @param content Content of the XML document.
 * @return The created XML document./*  w w  w .  j  a va2s. co m*/
 */
public static Document createDocument(String root, NodeList content) {
    DocumentBuilder docBuilder = createDocumentBuilder();
    Document document = docBuilder.newDocument();
    Element rootElement = document.createElement(root);
    document.appendChild(rootElement);

    for (int i = 0; i < content.getLength(); i++) {
        Node item = content.item(i);
        item = document.adoptNode(item.cloneNode(true));
        rootElement.appendChild(item);
    }
    return document;
}