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:Main.java

private static void addEntry(String file_path, String operation, String key, String value) {

    Document doc = getDocument(file_path);
    if (doc == null) {
        return;/*from   w w w .j a  va2 s .  c om*/
    }

    Element entry_elem = doc.createElement("Entry");
    entry_elem.setAttribute("Type", operation);
    entry_elem.setAttribute("Key", key);
    if (operation.equals("STORE")) {
        entry_elem.appendChild(doc.createTextNode(value));
    }

    Element log_element = (Element) doc.getElementsByTagName("Log").item(0);
    log_element.appendChild(entry_elem);

    saveFile(file_path, doc);

}

From source file:Main.java

/**
 * Add a RSS 2.0 Item to your channel node <channel>
 * Note: None of the parameter is supposed to be NULL
 *
 * @param XMLDocument the current XML Document
 * @param channelRoot your channel node/*w ww. j  ava 2s .  com*/
 * @param title title of your entry
 * @param link link to further information
 * @param guid globally unique identifier - same as link
 * @param pubDate date of the new entry
 * @param text text of the item
 */
public static void addRSSItem(Document XMLDocument, Node channelRoot, String title, String link, String guid,
        String pubDate, String text) {
    // Create <item> node
    Node entry = XMLDocument.createElement("item");

    // Set title node beneath <item>
    Node subentry = XMLDocument.createElement("title");
    subentry.appendChild(XMLDocument.createTextNode(title));
    entry.appendChild(subentry);

    // Set link node beneath <item>
    subentry = XMLDocument.createElement("link");
    subentry.appendChild(XMLDocument.createTextNode(link));
    entry.appendChild(subentry);

    // Set guid node beneath <item>
    subentry = XMLDocument.createElement("guid");
    subentry.appendChild(XMLDocument.createTextNode(guid));
    entry.appendChild(subentry);

    // Set pubDate beneath <item>
    subentry = XMLDocument.createElement("pubDate");
    subentry.appendChild(XMLDocument.createTextNode(pubDate));
    entry.appendChild(subentry);

    // Set last message node beneath <host>
    subentry = XMLDocument.createElement("description");
    subentry.appendChild(XMLDocument.createTextNode(text));
    entry.appendChild(subentry);

    channelRoot.appendChild(entry);
}

From source file:Main.java

public static void createElementAndAppend(String name, String value, Document doc, Element appendeeElement,
        String attributeName, String attributeValue) {
    if (value == null || value.equals("")) {

        log.info("XMLUtil.createElementAndAppend()  value == null for name = " + name + ".");
        value = "";
    }/*  ww w.j ava2 s .  c om*/
    Element newElement = doc.createElement(name);
    Text text = doc.createTextNode(value);
    newElement.appendChild(text);
    if (attributeName != null && !attributeName.equals("")) {
        newElement.setAttribute(attributeName, attributeValue);
    }

    appendeeElement.appendChild(newElement);
}

From source file:com.marklogic.client.test.DeleteSearchTest.java

public static void writeDoc() throws Exception {
    Document domDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element root = domDocument.createElement("root");
    root.setAttribute("xml:lang", "en");
    root.setAttribute("foo", "bar");
    root.appendChild(domDocument.createElement("child"));
    root.appendChild(domDocument.createTextNode("mixed"));
    domDocument.appendChild(root);/*  w  ww .ja va 2  s .c  o m*/

    @SuppressWarnings("unused")
    String domString = ((DOMImplementationLS) DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .getDOMImplementation()).createLSSerializer().writeToString(domDocument);

    XMLDocumentManager docMgr = Common.client.newXMLDocumentManager();
    docMgr.write(docId, new DOMHandle().with(domDocument));
}

From source file:it.unitn.disi.smatch.webapi.model.JSONHelper.java

public static Document wrapJsonToXml(JSONObject obj) throws ParserConfigurationException {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element root = doc.createElement(TAG_WEBAPI);
    Element el = doc.createElement(TAG_JSON_DATA);
    Text txt = doc.createTextNode(obj.toString());
    el.appendChild(txt);/*www .j  av  a2 s .c  om*/
    root.appendChild(el);
    doc.appendChild(root);
    return doc;
}

From source file:Main.java

/**
 * Adds a new {@link Element} with the given text to the given parent
 * {@link Element}.//  ww w.ja va  2 s . c o  m
 * 
 * @param doc the document to add to.
 * @param parent the parent element.
 * @param name the name of the new element.
 * @param value the text value.
 * @return the created element.
 */
public static Element addTextElement(Document doc, Node parent, String name, String value) {
    if (doc == null) {
        doc = parent.getOwnerDocument();
    }
    final Element elem = addElement(doc, parent, name);
    if (value != null) {
        elem.appendChild(doc.createTextNode(value));
    }
    return elem;
}

From source file:Main.java

private static Element constructDocument() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();
    Element rootElement = doc.createElement("root");
    doc.appendChild(rootElement);// w  w  w .j  a  v  a2s.co  m
    Element c1Element = doc.createElement("c1");
    Element c2Element = doc.createElement("c2");
    Element c3Element = doc.createElement("c3");
    Element gc1Element = doc.createElement("gc1_1");
    Element gc2Element = doc.createElement("gc1_2");
    Text textNode = doc.createTextNode("uncle_freddie");
    Element gc3Element = doc.createElement("gc2_1");

    rootElement.appendChild(c1Element);
    rootElement.appendChild(c2Element);
    rootElement.appendChild(c3Element);

    c1Element.appendChild(gc1Element);
    c1Element.appendChild(gc2Element);
    c2Element.appendChild(gc3Element);

    gc3Element.appendChild(textNode);

    return rootElement;
}

From source file:no.kantega.commons.util.XMLHelper.java

public static Element setChildText(Document doc, Element parent, String name, String value) {
    Element child = getChildByName(parent, name);
    if (child == null) {
        child = doc.createElement(name);
        child.appendChild(doc.createTextNode(value == null ? "" : value));
        parent.appendChild(child);//from  ww w . ja v a  2 s  .  c om
    } else {
        Node text = child.getFirstChild();
        if (text != null) {
            text.setNodeValue(value);
        } else {
            child.appendChild(doc.createTextNode(value == null ? "" : value));
        }
    }
    return child;
}

From source file:Main.java

/** Goes through and adds newlines and indent to the current node and all its children
 * @param current the current node//w  w  w  .  j  a v  a 2  s  . c o  m
 * @param indent the current level of indent this is increased recursively*/
private static void addFormatting(Document doc, Node current, String indent) {

    // go through each of the children adding space as required
    Node child = current.getFirstChild();
    String childIndent = indent + "\t";
    while (child != null) {
        Node nextChild = child.getNextSibling();
        if (child.getNodeType() != Node.TEXT_NODE) {
            // Only if we aren't a text node do we add the space
            current.insertBefore(doc.createTextNode("\n" + childIndent), child);
            if (child.hasChildNodes()) {
                addFormatting(doc, child, childIndent);
            }
            if (nextChild == null) {
                // Because this is the last child, we need to add some space after it
                current.appendChild(doc.createTextNode("\n" + indent));
            }
        }
        child = nextChild;
    }
}

From source file:es.itecban.deployment.security.client.ws.LogonWS.java

private static void addTextNode(Element element, String nodeName, String textValue) {
    // Get the document
    Document doc = element.getOwnerDocument();
    // Write the name
    Node resourceNameNode = doc.createElement(nodeName);
    element.appendChild(resourceNameNode);
    Text resourceNameText = doc.createTextNode(textValue);
    resourceNameNode.appendChild(resourceNameText);
}