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

/**
 * internal function for adding an element to the DOM tree.
 */// ww w.  j  ava2 s  .c  om
private static void addNode(Document doc, String elemName, String value) {
    Element root = doc.getDocumentElement();
    Element elem = doc.createElement(elemName);
    elem.appendChild(doc.createTextNode(value));
    root.appendChild(elem);
}

From source file:Main.java

/**
 * Creates a new element, with the given text inside that element.
 * //from w w  w  .j  ava2s  . c  o  m
 * @param document the document into which the element is added.
 * @param parentElement The parent element of the element-to-be-added.
 * @param newElementName name of new element (tag-name).
 * @param textString the text to be written inside the newly created element.
 * @return The new element.
 */
public static Element addElementWithText(Document document, Element parentElement, String newElementName,
        String textString) {
    Element newElement = document.createElement(newElementName);
    Text text = document.createTextNode(textString);
    newElement.appendChild(text);
    parentElement.appendChild(newElement);
    return newElement;
}

From source file:Main.java

public static void hashMapToXML222(String xmlFile, String xpath, HashMap hashmap) {
    try {//from w  ww .  j av  a 2s.  c  o m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.newDocument();

        Element rootNode = document.createElement(xpath);
        document.appendChild(rootNode);

        Set set = hashmap.entrySet();
        Iterator i = set.iterator();

        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            Element em = document.createElement(me.getKey().toString());
            em.appendChild(document.createTextNode(me.getValue().toString()));
            rootNode.appendChild(em);
            // System.out.println("write " +
            // me.getKey().toString() + "="
            // + me.getValue().toString());
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        FileOutputStream fo = new FileOutputStream(xmlFile);
        StreamResult result = new StreamResult(fo);
        transformer.transform(source, result);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Utils.java

public static Element createNewElementAndSet(Document document, Element parent, String childElement,
        String childValue) {/*from  w w  w .j  ava2 s .  co  m*/
    Element child = (Element) document.createElement(childElement);
    parent.appendChild(child);
    child.setNodeValue(childValue);
    child.appendChild(document.createTextNode(childValue));
    return child;
}

From source file:Main.java

/** Creates a new DOM. */
public static Document createDocument(final String rootName) {
    if (docBuilder == null) {
        try {/*  w  ww.j  ava 2  s  .co  m*/
            docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (final ParserConfigurationException exc) {
            exc.printStackTrace();
            return null;
        }
    }
    final Document doc = docBuilder.newDocument();
    if (rootName != null)
        doc.appendChild(doc.createElement(rootName));
    return doc;
}

From source file:Main.java

/**
 * Adds an element to a document in memory
 * /* w  w w  . j  a  v  a2  s.c o m*/
 * @param doc
 *            Document where element will be added
 * @param elementName
 *            The name of the element
 * 
 * @return org.w3c.dom.Element
 */
public static Element addElementToDocument(Document doc, String elementName) {
    // Creates the element and adds it to the document
    Element element = doc.createElement(elementName);
    doc.appendChild(element);

    return element;
}

From source file:Main.java

public static Element getPhoneTypeWithText(Document doc, String elementName, String areaCodeAndPhone) {
    Element elem_phone_type = doc.createElement(elementName);
    Element elem_phone = getElementWithText(doc, "Phone", areaCodeAndPhone);

    elem_phone_type.appendChild(elem_phone);

    return elem_phone_type;
}

From source file:Main.java

/**
 *
 * @param doc Document/*w ww.  j a v  a2  s . co  m*/
 * @param parent Node
 * @param name String
 * @param value int
 * @return Element
 */
public static Element appendChildNode(Document doc, Node parent, String name, int value) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(new Integer(value).toString()));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

/**
 *
 * @param doc Document/*from w ww  .  j  a va 2  s . c  o m*/
 * @param parent Node
 * @param name String
 * @param value String
 * @return Element
 */
public static Element appendChildNode(Document doc, Node parent, String name, String value) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(value));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static void addSimpleOutput(Element success, Document outDoc, String tagName, String value) {
    Element output = (Element) success.getElementsByTagName("output").item(0);
    Element outItem = outDoc.createElement(tagName);
    outItem.setTextContent(value);//from ww  w .  j ava  2  s.com
    output.appendChild(outItem);
}