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

private static Element createChildElement(Document document, Element parentElement, String elementTagName) {
    Element stringElement = document.createElement(elementTagName);
    parentElement.appendChild(stringElement);
    return stringElement;
}

From source file:Main.java

public static Element createSubelement(String name, Element parentElem, Document doc) {
    Element newElem = doc.createElement(name);

    if (parentElem != null)
        parentElem.appendChild(newElem);

    return newElem;
}

From source file:Main.java

/**
 * Appends another element as a child element.
 *
 * @param parent the parent element// w w w.java 2  s  .  co m
 * @param child the child element to append
 */
public static void appendElement(Element parent, Element child) {
    Node tmp = parent.getOwnerDocument().importNode(child, true);
    parent.appendChild(tmp);
}

From source file:Main.java

public static Element addElement(Document document, Element parentElement, String elementName) {
    Element childElement = document.createElement(elementName);
    parentElement.appendChild(childElement);
    return childElement;
}

From source file:Main.java

/**
 *
 * @param doc Document//from   www  .  j  a v  a  2s. c o  m
 * @param namespace String
 * @param parent Node
 * @param name String
 * @param value String
 * @return Element
 */
public static Element appendChildNode(Document doc, String namespace, org.w3c.dom.Node parent, String name,
        String value) {
    Element child = doc.createElementNS(namespace, name);
    child.appendChild(doc.createTextNode(value));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

/**
 * Print out the node in XML format//w  w w  . j  a va 2s. c o  m
 * 
 * @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 Element newElementWithAttrs(String tagName, String... attrNames) {
    Document document = newDocument();
    Element element = newElement(document, tagName);

    for (String attr : attrNames) {
        element.appendChild(document.createAttribute(attr));
    }/*from   w  ww.j  a  v  a 2  s  .c  o m*/

    return element;
}

From source file:Main.java

public static void hashMapToXML222(String xmlFile, String xpath, HashMap hashmap) {
    try {//from   ww  w. ja v  a 2s.co m
        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

/**
 * Appends a child element with the given name to the specified DOM element.
 *//*ww w  .j  a v a 2s  . c o  m*/
public static Element createChild(final Element el, final String name) {
    final Element child = el.getOwnerDocument().createElement(name);
    el.appendChild(child);
    return child;
}

From source file:Main.java

public static Element addElement(Element parent, Element childElement) {
    Node childNode = parent.getOwnerDocument().importNode(childElement, true);
    parent.appendChild(childNode);
    return parent;
}