Example usage for org.w3c.dom Document createElement

List of usage examples for org.w3c.dom Document createElement

Introduction

In this page you can find the example usage for org.w3c.dom Document createElement.

Prototype

public Element createElement(String tagName) throws DOMException;

Source Link

Document

Creates an element of the type specified.

Usage

From source file:Main.java

/**
 * Adds a text node to the provided element.
 * @param nodeName The name of the node.
 * @param doc The XML Document.// w w w. ja va  2 s. co m
 * @param parent The Element node to attach the text node to.
 * @param text The text to place in the node.
 */
private static void appendTextNode(String nodeName, Document doc, Element parent, String text) {
    if (text != null) {
        Element el = doc.createElement(nodeName);
        el.appendChild(doc.createTextNode(text));
        parent.appendChild(el);
    }
}

From source file:Main.java

public static Element addElement(final Document dom, final Element el, final String name, final String value) {
    Element newElement = dom.createElement(name);
    newElement.appendChild(dom.createTextNode(value));
    el.appendChild(newElement);//from   w w  w.j  a va  2  s  .c  o  m
    return el;
}

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./*ww w .  j a  v  a  2 s  .c o  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;
}

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  a  2 s  .  c  o m
        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

public static Document parseMetadataMap(Map<String, String> metainfo) throws Exception {
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
    DocumentBuilder bd = fact.newDocumentBuilder();
    Document doc = bd.newDocument();
    Element rt = doc.createElement("ROOT");
    doc.appendChild(rt);//from  w  w w  . ja v a 2 s  .c  o  m
    for (String key : metainfo.keySet()) {
        Element e1 = doc.createElement("param");
        e1.setAttribute("key", key);
        String value = metainfo.get(key);
        e1.setAttribute("value", value);
        rt.appendChild(e1);
    }
    return doc;
}

From source file:Main.java

public static Document documentify(ResultSet rs) throws ParserConfigurationException, SQLException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);/*from   www  .j  a v  a2s.  co m*/
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    while (rs.next()) {
        Element row = doc.createElement("Row");
        results.appendChild(row);
        for (int i = 1; i <= colCount; i++) {
            String columnName = rsmd.getColumnName(i);
            Object value = rs.getObject(i);
            Element node = doc.createElement(columnName);
            node.appendChild(doc.createTextNode(value.toString()));
            row.appendChild(node);
        }
    }
    return doc;
}

From source file:Main.java

/** @param text <code>null</code> removes 'subElement' completely */
public static Text setTextField(Element node, String subElement, String text) {
    NodeList nl = node.getElementsByTagName(subElement);
    if (nl == null || nl.getLength() == 0) {
        if (text == null)
            return null;
        Document d = node.getOwnerDocument();
        Element e = d.createElement(subElement);
        Text ans = d.createTextNode(text);
        e.appendChild(ans);/*from  ww w. jav  a2  s  .  c  o  m*/
        node.appendChild(e);
        return ans;
    } else if (text != null) {
        return setText(nl.item(0), text);
    } else {
        node.removeChild(nl.item(0));
        return null;
    }
}

From source file:Main.java

public static Element addElement(Document doc, Element parent, String nodeName) {
    Element el = null;/* w w  w  . j a  va 2  s  .  c o  m*/
    if (doc != null)
        el = doc.createElement(nodeName);

    if (parent != null)
        parent.appendChild(el);

    return el;
}

From source file:Main.java

public static Element addElementBefore(Document doc, Element sibling, String nodeName) {
    Element el = null;//from w  w w .  ja va  2 s  . com
    if (doc != null)
        el = doc.createElement(nodeName);

    if (sibling != null)
        sibling.getParentNode().insertBefore(el, sibling);

    return el;
}

From source file:Main.java

public static Document appendChildWithAttribute(Document doc, String nodeName, String parentNodeName,
        String attrName, String attrValue) {
    Element node = doc.createElement(nodeName);
    node.setAttribute(attrName, attrValue);
    doc.getElementsByTagName(parentNodeName).item(0).appendChild(node);
    return doc;/*from ww w .ja v a 2  s . co m*/
}