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

/**
 * Creates an {@link Element} containing the given text
 * /*  w w w  .  ja  v  a  2  s.  c  om*/
 * @param document the document to contain the new element
 * @param tagName the element's tag name (required)
 * @param text the text to set; can be <code>null</code> for none
 * @return a non-<code>null</code> element
 * @since 1.2.0
 */
public static Element createTextElement(final Document document, final String tagName, final String text) {
    final Element element = document.createElement(tagName);
    element.setTextContent(text);
    return element;
}

From source file:Main.java

/**
 * Adds an {@link Element} child to the given element.
 * <p>/*from ww w  . j  a  v a2 s  .com*/
 * If you only want to generate elements for a document that is to be
 * serialized, this method is fine.  If, however, you want to manipulate
 * the document, you should probably use
 * {@link #addElementChildTo(Element,String,String)}.
 *
 * @param parent The {@link Element} to add the {@link Element} child to.
 * @param tagName The tag name of the new {@link Element} child.
 * @return Returns A new {@link Element} with its <code>nodeName</code> set
 * to <code>tagName</code>, and <code>localName</code>, <code>prefix</code>,
 * and <code>namespaceURI</code> set to <code>null</code>.
 * @see #addElementChildTo(Element,String,String)
 */
public static Element addElementChildTo(Element parent, String tagName) {
    final Document doc = parent.getOwnerDocument();
    final Element child = doc.createElement(tagName);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static synchronized void createStorage() throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement(MESSAGES);
    doc.appendChild(rootElement);/*from  w w w. j ava  2  s.c om*/

    Transformer transformer = getTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(STORAGE_LOCATION));
    transformer.transform(source, result);
}

From source file:Main.java

public static Node createNode(Document document, String nodeName, String nodeValue) throws Exception {
    Element nodeElement = document.createElement("Property");
    nodeElement.setAttribute("name", nodeName);
    Element nodeValueElement = document.createElement("Value");
    nodeValueElement.setTextContent(nodeValue);
    nodeElement.appendChild(nodeValueElement);
    return nodeElement;
}

From source file:Main.java

/**
 * Creates a DOM element// w w  w  . ja  va  2s. co m
 * @param parent parent DOM element
 * @param tagName element name
 * @return a DOM element
 */
public static Element createElement(Element parent, String tagName) {
    Document doc = parent.getOwnerDocument();
    Element e = doc.createElement(tagName);
    parent.appendChild(e);
    return e;
}

From source file:Main.java

public static Element createElmWithText(Document doc, String tagName, String text) {
    Element elm = doc.createElement(tagName);
    text = text != null ? text.trim() : "";
    Node node = (text.indexOf('<') >= 0 || text.indexOf('\n') >= 0) ? doc.createCDATASection(text)
            : doc.createTextNode(text);/*from w  w w .  ja  va 2 s .  c o m*/
    elm.appendChild(node);
    return elm;
}

From source file:Main.java

public static synchronized void createIdStorage() throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement(IDS);
    doc.appendChild(rootElement);/* w ww .j a  v a  2s . c om*/

    Transformer transformer = getTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(ID_STORAGE_LOCATION));
    transformer.transform(source, result);
}

From source file:Main.java

public static Document createDocument(String paramString) throws Exception {
    DocumentBuilderFactory localDocumentBuilderFactory = getDocumentBuilderFactoryInstance();
    DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
    Document localDocument = localDocumentBuilder.newDocument();
    Element localElement = localDocument.createElement(paramString);
    localDocument.appendChild(localElement);
    return localDocument;
}

From source file:Main.java

public static void buildSkeleton() {
    try {//w  ww  .jav  a 2s. c o  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBldr = dFactory.newDocumentBuilder();

        Document doc = dBldr.newDocument();
        Element rootElement = doc.createElement("hooks");
        doc.appendChild(rootElement);

        write(doc);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Creates a DOM text element//from   w w  w.  j  a  v  a 2 s.  c o m
 * @param parent parent DOM element
 * @param tagName element name
 * @param text element text
 * @return a DOM element
 */
public static Element createTextElement2(Element parent, String tagName, String text) {
    Document doc = parent.getOwnerDocument();
    Element e = doc.createElement(tagName);
    e.appendChild(doc.createTextNode(text));
    parent.appendChild(e);
    return e;
}