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

/**
 * Creates a text node in an XML document
 * /*from  w w w.j  av  a 2 s .c  o m*/
 * @param d
 *            the "mother" document of the created text node
 * @param text
 *            contents of the text node
 * @return a new text node
 */
public static Text createText(Document d, String text) {
    return d.createTextNode(text);
}

From source file:Main.java

public static void nodeSubInsert(Element root, String table, String queryType, String queryValue) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    Element type = doc.createElement(queryType);

    Text value = doc.createTextNode(queryValue);

    type.appendChild(value);/*from w  w  w  . ja  v a2 s  .  co m*/
    find.appendChild(type);

    print(doc);

}

From source file:Main.java

public static void createChildTextWithComment(Document doc, Element elem, String name, String value,
        String comment) {/*from   www  .  j a  v a 2  s .co  m*/
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(value == null ? "" : value));
    Comment c = doc.createComment(comment);
    elem.appendChild(c);
    elem.appendChild(child);

}

From source file:Main.java

public static void addToXMLDoc(Document _doc, Element _element, String _tag, String _text) {
    Element child = _doc.createElement(_tag);
    _element.appendChild(child);//from  w ww. j av  a2  s .  c om
    Text childText = _doc.createTextNode(_text);
    child.appendChild(childText);
}

From source file:Main.java

private static void insertNode(String nodeName, String nodeValue) {
    File f = new File(System.getProperty("user.dir") + "\\DBConf.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;//from  w  w  w  . ja v a2  s .c  o m
    try {
        builder = factory.newDocumentBuilder();
        Document doc = builder.parse(f);
        Element tempElem = doc.createElement(nodeName);
        Text child = doc.createTextNode(nodeValue);
        tempElem.appendChild(child);
        doc.getElementsByTagName("conf").item(0).appendChild(tempElem);
        DOMSource source = new DOMSource(doc);
        StreamResult Streamres = new StreamResult(new FileOutputStream(f));
        TransformerFactory.newInstance().newTransformer().transform(source, Streamres);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Adds a simple DOM Node like <tagName>text</tagName> to a given node in a document
 *
 * @param XMLDocument current XML document
 * @param rootElement element you want append the new node to
 * @param tagName node's tag name//from   w  ww.  j a v  a 2s  .  c om
 * @param text text in the node
 */
public static void addTextNode(Document XMLDocument, Node rootElement, String tagName, String text) {
    Element newNode = XMLDocument.createElement(tagName);
    newNode.appendChild(XMLDocument.createTextNode(text));
    rootElement.appendChild(newNode);
}

From source file:Main.java

/** @param text <code>null</code> removes 'subElement' completely */
public static Text setTextField(Element node, String subElement, String text) {
    NodeList nl = node.getElementsByTagName(subElement);
    if (nl == null || nl.getLength() == 0) {
        if (text == null)
            return null;
        Document d = node.getOwnerDocument();
        Element e = d.createElement(subElement);
        Text ans = d.createTextNode(text);
        e.appendChild(ans);/*from  ww w  .  j av a 2  s  .  c  o  m*/
        node.appendChild(e);
        return ans;
    } else if (text != null) {
        return setText(nl.item(0), text);
    } else {
        node.removeChild(nl.item(0));
        return null;
    }
}

From source file:Main.java

/**
 * Adds a text node to the provided element.
 * @param nodeName The name of the node.
 * @param doc The XML Document.//from  w  w  w  .j a v a2s  . co m
 * @param parent The Element node to attach the text node to.
 * @param text The text to place in the node.
 */
private static void appendTextNode(String nodeName, Document doc, Element parent, String text) {
    if (text != null) {
        Element el = doc.createElement(nodeName);
        el.appendChild(doc.createTextNode(text));
        parent.appendChild(el);
    }
}

From source file:Main.java

public static Element addElement(final Document dom, final Element el, final String name, final String value) {
    Element newElement = dom.createElement(name);
    newElement.appendChild(dom.createTextNode(value));
    el.appendChild(newElement);//from  w w w  .  ja  va  2  s . co m
    return el;
}

From source file:Main.java

/**
 * /*from   w  w w  . j  a  v  a2s  . c om*/
 * @param doc
 * @param elem
 * @param name
 * @param value
 * @param comment
 */
public static void createChildTextWithComment(Document doc, Element elem, String name, String value,
        String comment) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createTextNode(value == null ? "" : value));
    Comment c = doc.createComment(comment);
    elem.appendChild(c);
    elem.appendChild(child);

}