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 appendTextChild(Element parent, String tagname, String text) {
    if (text == null) {
        text = "";
    }//from   ww w.j a  va 2s .  c  o m
    Document doc = parent.getOwnerDocument();
    Element childElement = doc.createElement(tagname);
    Text textNode = doc.createTextNode(text);
    childElement.appendChild(textNode);
    parent.appendChild(childElement);
}

From source file:Main.java

/**
 * Creates ElementNode for given tag with particular text as content.
 *///from ww w.  j  a  va2 s . com
public static Element createTextNode(String tag, String content) throws ParserConfigurationException {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    Element node = doc.createElement(tag);
    Text t = doc.createTextNode(content);
    node.appendChild(t);

    return node;
}

From source file:Main.java

public static Element addANode(Document doc, String name, Node parent) {
    Element e = doc.createElement(name);
    parent.appendChild(e);/*from   w  w  w  .ja  v a 2  s .  c o m*/
    return e;
}

From source file:Main.java

/**
 * Creates and returns an element with the specified tag and text content
 * @param tag the tag of the element/* www. j ava2 s  .  c  o  m*/
 * @param textContent the text content of the element
 * @param xml the Document to use to create the element
 * @return The element with the specified tag and text content
 */
public static Element createElement(String tag, String textContent, Document xml) {
    Element element = xml.createElement(tag);
    //Sets the text content of the element
    element.setTextContent(textContent);
    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);/*from ww  w .  j a  va  2s  .  co  m*/
    parent.appendChild(e);
    return t;
}

From source file:Main.java

public static Node addTag(Document doc, String index, String value) {
    org.w3c.dom.Element tag = doc.createElement(index);
    tag.appendChild(doc.createTextNode(value));
    return doc.appendChild(tag);
}

From source file:Main.java

private static Element addPropertyNode(Document document, Element parent, String name) {
    Element element = document.createElement(PROPERTY_NODE);
    parent.appendChild(element);/* w  w w  . ja v a  2s  . com*/
    element.setAttribute(NAME_ATTR, name);
    return element;
}

From source file:Main.java

public static Element createNewTextElement(Node elem, String tag, String value) {// throws Exception {
    Document doc = elem.getOwnerDocument();
    Element res = doc.createElement(tag);
    Text content = doc.createTextNode(value);
    res.appendChild(content);/*from ww  w.  j  a  va2s .  c  om*/
    elem.appendChild(res);
    return res;
}

From source file:Main.java

/**
 * Start a new XML Document./* www. j  a  v  a2 s  . c  o m*/
 * @param rootName The name of the Document root Element (created here)
 * @return the Document
 * @throws DomException
 */
public static Document createXmlDocument(String rootName) throws Exception {
    DocumentBuilderFactory factory;

    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(false);

    Document document = factory.newDocumentBuilder().newDocument();
    Element root = document.createElement(rootName);

    document.appendChild(root);
    return document;
}

From source file:Main.java

public static Element createNewCDATAElement(Node elem, String tag, String value) {
    Document doc = elem.getOwnerDocument();
    Element res = doc.createElement(tag);
    CDATASection cdata = doc.createCDATASection(value);
    res.appendChild(cdata);/*from   w w w.j a v a 2  s .  com*/
    elem.appendChild(res);
    return res;
}