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

/**
 * Create a property element.  Do not append it though!
 * @param doc The document to use./*from   www  .jav  a 2 s  . co  m*/
 * @param name The name of the property.
 * @param value The value to add to the property.
 * @return The newly created property.
 */
public static Node createProperty(final Document doc, final String name, final String value) {
    final Node n = doc.createElement(name);
    n.appendChild(doc.createTextNode(value));
    return n;
}

From source file:Main.java

/**
 * Adds a simple DOM Node like <tagName>text</tagName> to a given node in a document
 *
 * @param XMLDocument current XML document
 * @param rootElement element you want append the new node to
 * @param tagName node's tag name// w  w w .  j  a va  2 s  .c  o  m
 * @param text text in the node
 */
public static void addTextNode(Document XMLDocument, Node rootElement, String tagName, String text) {
    Element newNode = XMLDocument.createElement(tagName);
    newNode.appendChild(XMLDocument.createTextNode(text));
    rootElement.appendChild(newNode);
}

From source file:Main.java

public static Element writeBooleanElement(Document document, String name, boolean value,
        Element parentElement) {/*from w  ww.  j  a va2  s  . c  o  m*/
    Element element = document.createElement(name);
    Text text = document.createTextNode(Boolean.toString(value));
    element.appendChild(text);
    parentElement.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * create an element from a document, that has some text content in it
 * @param doc - document// w  w  w. j  a v  a  2  s .  co m
 * @param name - element name
 * @param content - text content
 * @return element object
 */
public static Element createElement(Document doc, String name, String content) {
    Element e = doc.createElement(name);
    e.appendChild(doc.createTextNode(content));
    return e;
}

From source file:Main.java

/**
 * Print out the node in XML format/*from  www . j  a va2  s  . com*/
 * 
 * @param node
 * @param out
 */
public static void printNode(Node node, PrintStream out) {
    Document doc = new DocumentImpl();
    Element topLevel = doc.createElement("xml");
    doc.appendChild(topLevel);
    topLevel.appendChild(doc.importNode(node, true));
    OutputFormat format = new OutputFormat(doc);
    format.setLineWidth(65);
    format.setIndenting(true);
    format.setIndent(2);
    XMLSerializer serializer = new XMLSerializer(out, format);
    try {
        serializer.serialize(doc);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }

}

From source file:Utils.java

public static Element createNewContainer(Document document, Element parent, String childElement) {
    Element child = (Element) document.createElement(childElement);
    parent.appendChild(child);//  w  w w  .j  av a 2s .  co  m
    return child;
}

From source file:Main.java

public static Node createDocument(String rootElementName) {
    try {/*from  w w  w.j a  v a 2  s .  c  om*/
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder;
        docBuilder = docFactory.newDocumentBuilder();

        Document doc = docBuilder.newDocument();

        Element rootElement = doc.createElement(rootElementName);
        doc.appendChild(rootElement);

        return rootElement;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static Element createElement(String name) {
    Element element = null;/*  www.j  av a  2  s  .  c o  m*/

    Document document = createDocument();
    if (document != null) {
        element = document.createElement(name);
    }

    return element;
}

From source file:Main.java

/**
 * Create a child of the given node.//from w  w w  . j a v  a2s  .  c  om
 * 
 * @param doc
 *            a document
 * @param node
 *            a node
 * @param nodeName
 *            a node name
 * @return org.w3c.dom.Element
 */
public static Element createChildElement(Document doc, Node node, String nodeName) {
    Element element = doc.createElement(nodeName);
    node.appendChild(element);
    return element;
}

From source file:Utils.java

/**
 * Start a new XML Document./*from  ww  w . ja  va  2  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) {

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

    document.appendChild(root);
    return document;

}