Example usage for org.w3c.dom Document createTextNode

List of usage examples for org.w3c.dom Document createTextNode

Introduction

In this page you can find the example usage for org.w3c.dom Document createTextNode.

Prototype

public Text createTextNode(String data);

Source Link

Document

Creates a Text node given the specified string.

Usage

From source file:Utils.java

public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
        String value) {/*  w ww . jav  a2s  .  c om*/
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
    }
    Element ret = (Element) parent.getElementsByTagName(child).item(0);
    if (ret.getFirstChild() != null) {
        ret.removeChild(ret.getFirstChild());
    }
    ret.appendChild(document.createTextNode(value));
    return ret;
}

From source file:Main.java

public static void hashMapToXML222(String xmlFile, String xpath, HashMap hashmap) {
    try {/*from w  w w .  ja v  a  2  s. co  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:Main.java

/**
 * Same as {@link createChild(Node, String) createChild()}, but adds the
 * specified <code>text</code> to the newly created <code>Node</code>.
 * //  www  . ja v  a  2s. c o  m
 * @see #createChild(Node, String)
 * @param elem the <code>Node</code> to which the newly created 
 * <code>Node</code> will be appended to. Not allowed to be <code>null</code>.
 * @param elemName the name of the to-be-created <code>Node</code>, not allowed 
 * to be empty or <code>null</code>.
 * @param text the text-contents of the created/inserted node  
 * @return the created element
 * @exception IllegalArgumentException if <code>elem</code> and/ord
 * <code>elemName</code> are null (or empty in the case of <code>elemName</code>)
 */
public static Element createTextChild(Node node, String elemName, String text) {
    if (node == null || elemName == null || "".equals(elemName))
        throw new IllegalArgumentException("Arguments are not allowed to be null or empty");

    Document document = null;

    if (node instanceof Document) {
        document = (Document) node;
    } else if (node.getOwnerDocument() != null) {
        document = node.getOwnerDocument();
    }

    Element newChild = null;
    if (document != null) {
        newChild = document.createElement(elemName);
        node.appendChild(newChild);
        newChild.appendChild(document.createTextNode(text));
    }

    return newChild;
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemStrValue(Document aDocument, Element anElement, String aName, String aValue) {
    Element subElement;/*  www  .j  a  v  a  2s .  c  o m*/

    if ((StringUtils.isNotEmpty(aName)) && (StringUtils.isNotEmpty(aValue))) {
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(aValue));
        anElement.appendChild(subElement);
    }
}

From source file:Main.java

/**
 * /*from ww  w .j  ava  2s  . co  m*/
 */
public static Document createVmlDocument() {
    Document document = createDocument();

    Element root = document.createElement("html");
    root.setAttribute("xmlns:v", "urn:schemas-microsoft-com:vml");
    root.setAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office");

    document.appendChild(root);

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

    Element style = document.createElement("style");
    style.setAttribute("type", "text/css");
    style.appendChild(document.createTextNode("<!-- v\\:* {behavior: url(#default#VML);} -->"));

    head.appendChild(style);
    root.appendChild(head);

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

    return document;
}

From source file:Main.java

/***************************************************************************
 * Sets the value of the first child under the given element with the given
 * name. If the child has no nodes, one is created.
 * //from w w w  .  ja  v  a  2  s . c  om
 * @param doc
 * @param e
 * @param name
 * @param value
 * @return
 * @throws Exception
 **************************************************************************/
public static boolean setChildValueByName(Document doc, Element e, String name, String value) throws Exception {
    NodeList childNodes = e.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE)
            continue;

        if (node.getNodeName().equals(name)) {
            Node child = node.getFirstChild();
            if (child == null)
                ((Element) node).appendChild(doc.createTextNode(value));
            else
                child.setNodeValue(value);

            return true;
        }
    }

    return false;
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemIntValue(Document aDocument, Element anElement, String aName, int aValue) {
    Integer intObject;/*from w  w w  .j av a2s  .com*/
    Element subElement;

    if (StringUtils.isNotEmpty(aName)) {
        intObject = aValue;
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(intObject.toString()));
        anElement.appendChild(subElement);
    }
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemLongValue(Document aDocument, Element anElement, String aName, long aValue) {
    Long longObject;//from w  w w  .  jav a2  s.c  o m
    Element subElement;

    if (StringUtils.isNotEmpty(aName)) {
        longObject = aValue;
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(longObject.toString()));
        anElement.appendChild(subElement);
    }
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static void makeElemDoubleValue(Document aDocument, Element anElement, String aName, double aValue) {
    Element subElement;/*w  ww.  j a  v  a2s .  c  om*/
    Double doubleObject;

    if (StringUtils.isNotEmpty(aName)) {
        doubleObject = aValue;
        subElement = aDocument.createElement(aName);
        subElement.appendChild(aDocument.createTextNode(doubleObject.toString()));
        anElement.appendChild(subElement);
    }
}

From source file:Main.java

public static Element appendSingleValElementEncoded(Document owner, Element appendElement, String newElemName,
        String newElemVal) {//w w  w. j a  va 2s  . co  m
    Element newElem;
    newElem = owner.createElement(newElemName);
    if (newElemVal != null) {
        String encodedVal = "";
        try {
            encodedVal = URLEncoder.encode(newElemVal, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Text value = owner.createTextNode(encodedVal);
        newElem.appendChild(value);
        newElem.setAttribute("enc", "t");
        newElem.setAttribute("charSet", "UTF-8");
    }
    appendElement.appendChild(newElem);
    return (newElem);
}