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 Element addPropertiesNode(Document document, Element parent) {
    Element element = document.createElement(PROPERTIES_NODE);
    parent.appendChild(element);
    return element;
}

From source file:Main.java

/** Add a Text element to a Document */
public static Text addChildTextElement(Document doc, Node parent, String tag, String value) {

    Element e = doc.createElement(tag);
    Text t = doc.createTextNode(value);
    e.appendChild(t);
    parent.appendChild(e);/*from  w w  w.ja  v a  2  s  .c  o m*/
    return t;
}

From source file:Main.java

/**
 * create an element from a document, that has some text content in it
 * @param doc - document/*  ww w .j a  v  a 2  s  .c om*/
 * @param name - element name
 * @param content - text content
 * @return element object
 */
public static Element createElement(Document doc, String name, String content) {
    Element e = doc.createElement(name);
    e.appendChild(doc.createTextNode(content));
    return e;
}

From source file:Main.java

/**
 * Creates a DOM text element/*from   ww  w . j a v a 2  s  .  c  o m*/
 * @param parent parent DOM element
 * @param tagName element name
 * @param text element text
 * @return a DOM element
 */
public static Element createTextElement2(Element parent, String tagName, String text) {
    Document doc = parent.getOwnerDocument();
    Element e = doc.createElement(tagName);
    e.appendChild(doc.createTextNode(text));
    parent.appendChild(e);
    return e;
}

From source file:Main.java

public static void addNode(Document doc, Node parent, String ns, String name, String text, Map NS_MAP) {
    Element child = doc.createElementNS((String) NS_MAP.get(ns), ns + ":" + name);
    child.appendChild(doc.createTextNode(text));
    parent.appendChild(child);//  ww w . ja va 2 s .c o  m
}

From source file:Main.java

public static Element createElement(String name, Object value, Document doc) {
    Element element = doc.createElement(name);
    element.appendChild(doc.createTextNode(value.toString()));
    return element;
}

From source file:Main.java

public static Element createTextElement(final Document document, final String qName, final String content,
        final Optional<String> namespaceURI) {
    Element typeElement = createElement(document, qName, namespaceURI);
    typeElement.appendChild(document.createTextNode(content));
    return typeElement;
}

From source file:Main.java

public static void createTextChildElement(Document doc, Node node, String name, String value) {
    Element element = createChildElement(doc, node, name);
    element.appendChild(doc.createTextNode(value));
}

From source file:Main.java

public static void createXML222(String xmlFile, String xpath, String element, String data) {
    try {//from w w w .j  a v a2 s  .  c  om
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();
        Element rootElement = document.createElement(xpath);
        document.appendChild(rootElement);
        for (int i = 1; i <= 1; i++) {
            Element em = document.createElement(element);
            em.appendChild(document.createTextNode(data));
            rootElement.appendChild(em);
        }
        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

/**
 * Helper method for creating slot key-value pairs in the GnuCash XML structure.
 * <p>This method is only a helper for creating slots whose values are of string type</p>
 * @param doc {@link org.w3c.dom.Document} for creating nodes
 * @param key Slot key as string//  w  w w  .j av a  2s. co m
 * @param value Slot value as String
 * @return Element node containing the key-value pair
 */
public static Element createSlot(Document doc, String key, String value, String valueType) {
    Element slotNode = doc.createElement(TAG_SLOT);
    Element slotKeyNode = doc.createElement(TAG_SLOT_KEY);
    slotKeyNode.appendChild(doc.createTextNode(key));
    Element slotValueNode = doc.createElement(TAG_SLOT_VALUE);
    slotValueNode.setAttribute(ATTR_KEY_TYPE, valueType);
    slotValueNode.appendChild(doc.createTextNode(value));
    slotNode.appendChild(slotKeyNode);
    slotNode.appendChild(slotValueNode);

    return slotNode;
}