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 <K, V> String mapToXml(Map<K, V> map, String rootName, String childName, String keyName,
        String valueName) throws TransformerException, ParserConfigurationException {
    Document document = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().newDocument();

    Element root = document.createElement(rootName);
    document.appendChild(root);//  w w w.ja v  a2s.c  om

    for (Map.Entry<K, V> errorCodeReport : map.entrySet()) {
        Element s = document.createElement(childName);
        root.appendChild(s);

        s.setAttribute(keyName, errorCodeReport.getKey().toString());
        s.setAttribute(valueName, errorCodeReport.getValue().toString());
    }

    return getStringFromDocument(document);
}

From source file:Main.java

private static Node makeColumnList(Iterable<String> columns) throws ParserConfigurationException {
    final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

    final Element node = doc.createElement("visible-columns");

    for (String s : columns) {
        addNodeWithText(doc, node, "label", s);
    }//from w  w  w  . jav  a  2 s .com

    return node;
}

From source file:Main.java

private static Node createChildInternal(Document document, Node parent, String nodeName,
        String... attr_name_and_value) {
    Element newNode = document.createElement(nodeName);

    for (int i = 0; i < attr_name_and_value.length; i += 2) {
        String attrName = attr_name_and_value[i];
        String attrValue = attr_name_and_value[i + 1];
        newNode.setAttribute(attrName, attrValue);
    }/*from w w w  . j a  v a 2 s .com*/
    parent.appendChild(newNode);
    return newNode;
}

From source file:Main.java

public static void setChildText(Element e, String tagname, String newValue, boolean cdata) {
    Element child = getChildByTagName(e, tagname);

    if (child == null) {
        Document doc = e.getOwnerDocument();
        child = doc.createElement(tagname);
        e.appendChild(child);/*w w  w . ja  v  a2  s. c o  m*/
    }

    setElementText(child, newValue, cdata);
}

From source file:Main.java

public static void createChildTextWithComment(Document paramDocument, Element paramElement, String paramString1,
        String paramString2, String paramString3) {
    Element localElement = paramDocument.createElement(paramString1);
    localElement.appendChild(paramDocument.createTextNode((paramString2 == null) ? "" : paramString2));
    Comment localComment = paramDocument.createComment(paramString3);
    paramElement.appendChild(localComment);
    paramElement.appendChild(localElement);
}

From source file:Main.java

public static void createOptionalChildText(Document doc, Element elem, String name, String value) {
    if (value == null || value.length() == 0) {
        return;/*w  w  w. j a  v a 2 s  .c o m*/
    }
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(value));
    elem.appendChild(child);
}

From source file:Main.java

/**
 * Creates a DOM element node with the given name. This node must only be
 * used in the specified document./*  ww w.  java 2s . c o  m*/
 * 
 * @param d
 *            the "mother" document of the created node
 * @param name
 *            the name of the node
 * @return a node with the given name
 */
public static Element createElement(Document d, String name) {
    return d.createElement(name);
}

From source file:Main.java

/**
 * The method creates an element with specified name and attributes and appends it to the parent element.
 * This is a convenience method for often used sequence of calls.
 *
 * @param parent - the node where to append the new element.
 * @param name - the name of the element type being created.
 * @param attributes - string array of pairs attributes and their values.
 *     Each attribute must have a value, so the array must have even number of elements.
 * @return the newly created element./*from   w w  w  . j  a v  a  2s. c o m*/
 *
 * @throws ArrayIndexOutOfBoundsException in case of odd number of elements of the attribute array
 *    (i.e. the last attribute is missing a value).
 */
public static Element appendElement(Node parent, String name, String[] attributes) {
    Document doc = parent instanceof Document ? (Document) parent : parent.getOwnerDocument();
    Element element = doc.createElement(name);
    if (attributes != null) {
        int attrLen = attributes.length;
        for (int i = 0; i < attrLen; i += 2) {
            String attrName = attributes[i];
            String attrValue = attributes[i + 1];
            element.setAttribute(attrName, attrValue);
        }
    }
    parent.appendChild(element);
    return element;
}

From source file:Main.java

public static Document AppendContentToEnd(Document doc, String tag, String content) {
    if (tag == null || tag == "" || content == null || content == "")
        return doc;
    Element e = doc.createElement(tag);
    e.setTextContent(content);/*from   ww w. ja va2  s.com*/
    Node lastone = doc.getLastChild();
    lastone.appendChild(e);
    return doc;
}

From source file:Main.java

/**
 * Adds an element to another of a document in memory <br>
 * (It's supposed that the parent is one element of the document)
 * //from w w w .  ja  va  2s . c  om
 * @param doc
 *            Document of the parent element
 * @param parent
 *            Parent element where a child element will be added
 * @param elementName
 *            The name of the child element
 * 
 * @return org.w3c.dom.Element
 */
public static Element addElementChildToElement(Document doc, Element parent, String elementName) {
    // Creates the element and adds it to the parent
    Element element = doc.createElement(elementName);
    parent.appendChild(element);

    return element;
}