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 Element getCustomerContactTypeWithText(Document doc, String elementName, String name,
        String email) {/*from   w w w . ja  v a 2 s.  c  om*/

    Element elem_customer_contact_type = doc.createElement(elementName);

    Element elem_name = getElementWithText(doc, "Name", name);
    Element elem_email = getElementWithText(doc, "Email", email);

    elem_customer_contact_type.appendChild(elem_name);
    elem_customer_contact_type.appendChild(elem_email);

    return elem_customer_contact_type;
}

From source file:Main.java

public static Document addRoot(Document dataDoc, String elementName) {
    Element oldRoot = dataDoc.getDocumentElement();
    Element newRoot = dataDoc.createElement(elementName);
    dataDoc.removeChild(oldRoot);/*from ww w  . j av  a2 s  .  c  om*/
    newRoot.appendChild(oldRoot);
    dataDoc.appendChild(newRoot);
    return dataDoc;
}

From source file:Main.java

public static Document CreateDom() {
    Document newdoc = getDocumentBuilder().newDocument();
    //newdoc..PreserveWhitespace = true;
    Element childnode = newdoc.createElement("root");
    newdoc.appendChild(childnode);/* w  w w  . ja  va2 s  .c o  m*/
    return newdoc;
}

From source file:Main.java

/**
 * Create a child of the given node at the given index.
 * //ww w.j  av  a 2  s  .c  o m
 * @param doc
 *            a document
 * @param element
 *            an element
 * @param index
 *            an index
 * @param nodeName
 *            a node name
 * @return org.w3c.dom.Element
 */
public static Element createChildElement(Document doc, Element element, int index, String nodeName) {
    Element element2 = doc.createElement(nodeName);
    try {
        NodeList childList = element.getElementsByTagName(nodeName);
        Node child = childList.item(index);
        element.insertBefore(element2, child);
    } catch (Exception e) {
        element.appendChild(element2);
    }
    return element2;
}

From source file:Main.java

/**
 *
 * @param doc Document/* w  w  w.j a  v  a  2  s  . c om*/
 * @param parent Node
 * @param name String
 * @return Element
 */
public static Element appendChildNode(Document doc, Node parent, String name) {
    Element child = doc.createElement(name);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

/**
 * Changes the tag name of an element.//  w w w .j a  v a 2  s.  c o m
 * 
 * @param elem Element which name should be changed
 * @param newName new tag name of the element
 * @return true if the name was changed successfully or false otherwise
 */
static public boolean changeTagName(Element elem, String newName) {
    if (elem == null)
        return false; // not Found!
    Document doc = elem.getOwnerDocument();
    Element newElem = doc.createElement(newName);

    // Copy the attributes to the new element
    NamedNodeMap attrs = elem.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        newElem.getAttributes().setNamedItem(attr2);
    }

    // Copy all Child Elements
    for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling())
        newElem.appendChild(node.cloneNode(true));
    // insert
    Node parent = elem.getParentNode();
    parent.replaceChild(newElem, elem);
    return true;
}

From source file:Main.java

public static void addChild(final Document doc, Element parent, final String childName,
        final String childValue) {
    Element child = doc.createElement(childName);
    Text text = doc.createTextNode(childValue);
    child.appendChild(text);/*from  ww  w  .  j a v a 2s. com*/
    parent.appendChild(child);
}

From source file:Main.java

public static Document addXMLItems(File xmlfile, String tag) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc;
    if (xmlfile.length() == 0) {
        doc = builder.newDocument();/*  ww  w . j av  a 2s  .  com*/
        Element rootElement = doc.createElement(tag + "s");
        doc.appendChild(rootElement);
    } else {
        doc = builder.parse(xmlfile);
    }
    return doc;
}

From source file:Main.java

/**
 * Add the element to the parent by the node name
 *
 * @param parent//ww w.  j  a  v a2  s . com
 * @param tagName
 */
public static void addElement(Element parent, String tagName) {
    Document doc = parent.getOwnerDocument();
    Element child = doc.createElement(tagName);
    parent.appendChild(child);
}

From source file:Main.java

/**
 * add element to parent withe the specified value by the node name
 *
 * @param parent/*from www  .j  a v  a  2s.co m*/
 * @param tagName
 * @param text
 */
public static void addElement(Element parent, String tagName, String text) {
    Document doc = parent.getOwnerDocument();
    Element child = doc.createElement(tagName);
    setElementValue(child, text);
    parent.appendChild(child);
}