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 Document newDocument(String rootName) throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    doc.appendChild(doc.createElement(rootName));

    return (doc);
}

From source file:Main.java

/**
 * Returns a document with a HTML node containing a HEAD and BODY node.
 *//*from  w w w. j a va  2s  .co  m*/
public static Document createHtmlDocument() {
    Document document = createDocument();

    Element root = document.createElement("html");

    document.appendChild(root);

    Element head = document.createElement("head");
    root.appendChild(head);

    Element body = document.createElement("body");
    root.appendChild(body);

    return document;
}

From source file:Main.java

/**
 * Utility method to add an attribute to a given element
 *
 * @param document/*from w w w . j a  v a 2s . co  m*/
 * @param elementName
 * @param parentNode
 */
public static Element addChildElement(Document document, String elementName, Node parentNode) {
    Element elt = document.createElement(elementName);
    parentNode.appendChild(elt);
    return elt;
}

From source file:Main.java

public static Element createParagraphElement(String name, String content, Document doc) {

    Element element = doc.createElement(name);

    BufferedReader br = new BufferedReader(new StringReader(content));

    try {/*w  ww.  j  a  v a 2  s .  c  om*/
        while (br.ready()) {
            // Create an element for this node
            String p = br.readLine();

            // To avoid an infinite loop
            if (p == null)
                break;
            Element pe = doc.createElement("p");

            //Add text to paragraph node
            Text t = doc.createTextNode(p);
            pe.appendChild(t);
            element.appendChild(pe);
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    return element;
}

From source file:Main.java

public static void appendString(Element xmlElement, String tagName, String value) {
    if (value == null)
        return;//  w  ww.  j  a  v  a 2 s .  c o m
    Document parentDocument = xmlElement.getOwnerDocument();
    Element newElement = parentDocument.createElement(tagName);
    newElement.setTextContent(value);
}

From source file:Main.java

private static Element createChildElement(Document document, Element parentElement, String elementTagName) {
    Element stringElement = document.createElement(elementTagName);
    parentElement.appendChild(stringElement);
    return stringElement;
}

From source file:Main.java

public static Element writeStringElement(Document document, String name, String value, Element parentElement) {
    Element element = document.createElement(name);
    if (value == null) {
        value = "null";
    }/*from   w  w  w .  ja  v  a2 s .  co m*/
    Text text = document.createTextNode(value);
    element.appendChild(text);
    parentElement.appendChild(element);
    return element;
}

From source file:Main.java

public static Document blankDocument(String paramString) throws Exception {
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    localDocumentBuilderFactory.setIgnoringComments(false);
    localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
    localDocumentBuilderFactory.setValidating(false);
    localDocumentBuilderFactory.setCoalescing(false);
    DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
    Document localDocument = localDocumentBuilder.newDocument();
    Element localElement = localDocument.createElement(paramString);
    localDocument.appendChild(localElement);
    return localDocument;
}

From source file:Main.java

License:asdf

public static void replacePerson(Document doc, String name, String phone, String email) {
    Element newPersonNode = doc.createElement("person");

    Element nameNode = doc.createElement("name");
    newPersonNode.appendChild(nameNode);
    Text nametextNode = doc.createTextNode(name);
    nameNode.appendChild(nametextNode);//ww  w .  j  a v a 2s.c om

    Element phoneNode = doc.createElement("phone");
    newPersonNode.appendChild(phoneNode);
    Text phonetextNode = doc.createTextNode(phone);
    phoneNode.appendChild(phonetextNode);

    Element emailNode = doc.createElement("email");
    newPersonNode.appendChild(emailNode);
    Text emailtextNode = doc.createTextNode(email);
    emailNode.appendChild(emailtextNode);

    Element root = doc.getDocumentElement();
    Element oldPersonNode = (Element) root.getFirstChild();
    root.replaceChild(newPersonNode, oldPersonNode);
}

From source file:Main.java

public static Element addTextNode(Document xmlDoc, Element ndParent, String nodeName, String nodeValue) {

    Element ndNode = xmlDoc.createElement(nodeName);
    Text ndTextNode = xmlDoc.createTextNode(nodeName);
    ndTextNode.setData(nodeValue);/*  www  .j a v a 2s  . c  om*/
    ndNode.appendChild(ndTextNode);
    ndParent.appendChild(ndNode);
    return ndNode;

}