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

public static Document createDocument(String mainType, String customType) throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();
    document.setXmlStandalone(false);//from w w  w  . j  av  a  2 s.  c o  m
    Element sync = document.createElement("Sync");
    document.appendChild(sync);
    Element entity = document.createElement("Entity");
    entity.setAttribute("mainType", mainType);
    if (customType.length() > 0)
        entity.setAttribute("customType", customType);
    document.getElementsByTagName("Sync").item(0).appendChild(entity);
    return document;
}

From source file:Main.java

public static Document createDocument(String rootElement) throws ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    Document document = dbFactory.newDocumentBuilder().newDocument();
    document.appendChild(document.createElement(rootElement));
    return document;
}

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

From source file:Main.java

/**
 * //from   ww w  .  ja  v a2s  . com
 * @param doc
 * @param elem
 * @param name
 * @param value
 * @param comment
 */
public static void createChildTextWithComment(Document doc, Element elem, String name, String value,
        String comment) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(value == null ? "" : value));
    Comment c = doc.createComment(comment);
    elem.appendChild(c);
    elem.appendChild(child);

}

From source file:Main.java

private static Node addNodeWithText(final Document doc, final Element parent, String name, String text) {
    final Element child = doc.createElement(name);
    child.setTextContent(text);/*from  w ww .  ja  v  a  2  s.  c om*/
    return parent.appendChild(child);
}

From source file:Main.java

static public void appendDateNode(Document owner, Element appendElement, String name, Date date) {
    Element dateElem = owner.createElement(name);
    appendElement.appendChild(dateElem);
    appendSingleValElement(owner, dateElem, "month", Integer.toString(date.getMonth() + 1));
    appendSingleValElement(owner, dateElem, "day", Integer.toString(date.getDate()));
    appendSingleValElement(owner, dateElem, "year", Integer.toString(date.getYear() + 1900));
    appendSingleValElement(owner, dateElem, "hour", Integer.toString(date.getHours()));
    appendSingleValElement(owner, dateElem, "minute", Integer.toString(date.getMinutes()));
    appendSingleValElement(owner, dateElem, "second", Integer.toString(date.getSeconds()));
}

From source file:Main.java

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

From source file:Main.java

public static String convertResultSetToXML(ResultSet rs)
        throws SQLException, ParserConfigurationException, TransformerException {
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);//  w ww . j av  a 2s. c om

    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);
            if (value != null) {
                Element node = doc.createElement(columnName.replaceAll("\\(", "_").replaceAll("\\)", "_"));
                node.appendChild(doc.createTextNode(value.toString()));
                row.appendChild(node);
            }
        }
    }

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    String output = writer.getBuffer().toString().replaceAll("\n|\r", "");

    return output;
}

From source file:Main.java

public static void createFile(String file_path) {

    try {/*ww w.j av a2s  .  co  m*/
        DocumentBuilderFactory doc_Factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder doc_builder = doc_Factory.newDocumentBuilder();

        // root elements
        Document doc = doc_builder.newDocument();
        Element root_element = doc.createElement("Log");
        doc.appendChild(root_element);

        saveFile(file_path, doc);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

/**
 * create an element from a document, that has some text content in it
 * @param doc - document/*from  w w w .  j  av a 2s.com*/
 * @param name - element name
 * @param attributes - attribute map
 * @return element object
 */
public static Element createElement(Document doc, String name, Map<String, String> attributes) {
    Element e = doc.createElement(name);
    for (String attribute : attributes.keySet())
        e.setAttribute(attribute, attributes.get(attribute));
    return e;
}