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 void addChildValue(Document doc, Node parent, String childName, String childValue) {
    Element element = doc.createElement(childName);
    Text text = doc.createTextNode(childValue);
    element.appendChild(text);/*from w  w  w.  j  av a  2s  .co  m*/
    parent.appendChild(element);
}

From source file:Main.java

/**
 * Inserts a new value for an XML tag specified by <code>tagName</code> name
 * in a <code>Document</code> object.
 * /* www. java 2 s.c  om*/
 * @param doc
 *            Document object.
 * @param tagName
 *            Name of the tag as String.
 * @param tagValue
 *            Value of the tag as String.
 */
public static Element insertTagValue(Document doc, String tagName, String tagValue) {
    Element element = doc.createElement(tagName);
    doc.getDocumentElement().appendChild(element);
    if (tagValue != null) {
        element.appendChild(doc.createTextNode(tagValue));
    }
    return element;
}

From source file:Main.java

/**
 * creates two elements: - one for the tag - one for the value and appends the
 * value as the child of the tag element returns a reference to the tag
 * element. Both elements are created in the document.
 */// www. j  a  va 2 s  . c  o m
public static Element createTaggedElement(Document doc, String tag, String value) {
    Element tagElement = doc.createElement(tag);
    Text valueElement = doc.createTextNode(value);
    tagElement.appendChild(valueElement);
    return tagElement;
}

From source file:Main.java

static public synchronized Element createDocument(String rootElemName) {
    // create document
    Document doc = createDocument();
    Element root = doc.createElement(rootElemName);
    doc.appendChild(root);//from   w  ww.  j a v a2 s  . c om
    return root;
}

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 void addNode(Document doc, Node parent, String name, String text) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(text));
    parent.appendChild(child);/*from  w  w  w.  j av  a  2s . c o  m*/
}

From source file:Main.java

private static Element createElement(Document document, String name, String textContent) {
    Element element = document.createElement(name);
    element.setTextContent(textContent);
    return element;
}

From source file:Main.java

static public Document createDefaultMessage(String messagetype) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentbuilder = null;
    try {//from w  ww .  ja  va  2s . c  o  m
        documentbuilder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }

    Document doc = documentbuilder.newDocument();
    Element root = doc.createElement("message");
    doc.appendChild(root);
    long timestamp = System.currentTimeMillis();
    root.setAttribute("timestamp", Long.toString(timestamp));
    root.setAttribute("type", messagetype);
    return doc;
}

From source file:Main.java

/**
 * Gets the arg element/* w ww.  j a  v a  2s  .  c o  m*/
 * @param text the text content
 * @param xml the Document to use to create the element
 * @return the arg element
 */
public static Element getArg(String text, Document xml, boolean value) {
    Element arg = xml.createElement("arg");
    if (value)
        arg.setAttribute("value", text);
    else
        arg.setTextContent(text);
    return arg;
}

From source file:Main.java

public static void edit(Document doc) {
    Element element = doc.getDocumentElement();
    Element element2 = doc.createElement("newname");
    NamedNodeMap attrs = element.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        element2.getAttributes().setNamedItem(attr2);
    }//from ww w  .  java  2 s . c o  m
    while (element.hasChildNodes()) {
        element2.appendChild(element.getFirstChild());
    }
    element.getParentNode().replaceChild(element2, element);
}