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

private static Element makePersonNode(Document doc, String name, String phone) {
        Element nameNode = doc.createElement("name");
        Text nametextNode = doc.createTextNode(name);
        nameNode.appendChild(nametextNode);

        Element phoneNode = doc.createElement("phone");
        Text phonetextNode = doc.createTextNode(phone);
        phoneNode.appendChild(phonetextNode);

        Element personNode = doc.createElement("person");
        personNode.appendChild(nameNode);
        personNode.appendChild(phoneNode);
        return (personNode);
    }/*from w  ww  . java  2s .c  om*/

From source file:Main.java

/**
 * This method is used for updating the value of a tag in a
 * <code>Document</code> object.
 * //from   w  w  w .  j a  v  a  2s .  c o m
 * @param doc
 *            Document object
 * @param tagName
 *            name of the tag
 * @param tagValue
 *            the updated value of the tag
 */
public static void replaceTagValue(Document doc, String tagName, String tagValue) {
    NodeList nodeList = doc.getElementsByTagName(tagName);
    int j = nodeList.getLength();
    Node node;
    for (int i = 0; i < j; i++) {
        Node newNode = doc.createTextNode(tagValue);
        node = nodeList.item(i);
        if (node.getFirstChild() != null) {
            node.replaceChild(newNode, node.getFirstChild());
        } else {
            node.appendChild(newNode);
        }
    }
}

From source file:Main.java

public static void addNewElementWithAttribute(Document xmlDoc, Node node, String elementName,
        String elementValue, String attrName, String attrValue) {
    // element//from  w ww  .  j  ava 2s  .  c  o m
    Node newVal = xmlDoc.createElement(elementName);
    Node newValText = xmlDoc.createTextNode(elementValue);
    newVal.appendChild(newValText);
    // attribute
    addAttribute(xmlDoc, newVal, attrName, attrValue);
    // add to node
    node.appendChild(newVal);
}

From source file:Main.java

License:asdf

public static void replacePerson(Document doc, String name, String phone, String email) {
    Element newPersonNode = doc.createElement("person");

    Element nameNode = doc.createElement("name");
    newPersonNode.appendChild(nameNode);
    Text nametextNode = doc.createTextNode(name);
    nameNode.appendChild(nametextNode);// w w w.  ja  va  2s .  c o m

    Element phoneNode = doc.createElement("phone");
    newPersonNode.appendChild(phoneNode);
    Text phonetextNode = doc.createTextNode(phone);
    phoneNode.appendChild(phonetextNode);

    Element emailNode = doc.createElement("email");
    newPersonNode.appendChild(emailNode);
    Text emailtextNode = doc.createTextNode(email);
    emailNode.appendChild(emailtextNode);

    Element root = doc.getDocumentElement();
    Element oldPersonNode = (Element) root.getFirstChild();
    root.replaceChild(newPersonNode, oldPersonNode);
}

From source file:Main.java

/**
 *
 * @param doc Document//  w  w w .jav a 2 s. c o  m
 * @param namespace String
 * @param parent Node
 * @param name String
 * @param value String
 * @return Element
 */
public static Element appendChildNode(Document doc, String namespace, org.w3c.dom.Node parent, String name,
        String value) {
    Element child = doc.createElementNS(namespace, name);
    child.appendChild(doc.createTextNode(value));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static Element appendSingleValElement(Document owner, Element appendElement, String newElemName,
        String newElemVal) {//from  www. java2s  .c o  m
    Element newElem;
    newElem = owner.createElement(newElemName);
    if (newElemVal != null) {
        Text value = owner.createTextNode(newElemVal);
        newElem.appendChild(value);
    }
    appendElement.appendChild(newElem);
    return (newElem);
}

From source file:Main.java

/**
 * Add a RSS 2.0 Channel Information to a given node - which is your channel node <channel>
 * Note: None of the parameter is supposed to be NULL
 *
 * @param XMLDocument current XML Document
 * @param channelRoot the channel node to which you want the information to be attached to
 * @param title title of your channel//from   w  w  w  . java2  s  .c  o m
 * @param link  link to your channel home
 * @param description description of your channel
 */
public static void addRSSChannelInformation(Document XMLDocument, Node channelRoot, String title, String link,
        String description) {
    // Title node
    Node entry = XMLDocument.createElement("title");
    entry.appendChild(XMLDocument.createTextNode(title));
    channelRoot.appendChild(entry);

    // Link node
    entry = XMLDocument.createElement("link");
    entry.appendChild(XMLDocument.createTextNode(link));
    channelRoot.appendChild(entry);

    // Description node
    entry = XMLDocument.createElement("description");
    entry.appendChild(XMLDocument.createTextNode(description));
    channelRoot.appendChild(entry);

    // lastBuildDate node
    entry = XMLDocument.createElement("lastBuildDate");
    entry.appendChild(XMLDocument.createTextNode(
            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date())));
    channelRoot.appendChild(entry);

    // language node
    entry = XMLDocument.createElement("language");
    entry.appendChild(XMLDocument.createTextNode("en-gb"));
    channelRoot.appendChild(entry);
}

From source file:Main.java

public static void appendChild(Document document, Node root, String nsURI, String name, String value) {
    Node node = document.createElementNS(nsURI, name);
    node.appendChild(document.createTextNode(value != null ? value : ""));
    root.appendChild(node);/*  w  ww . j a v a2 s  . co  m*/
    return;
}

From source file:Utils.java

public static Element createNewElementAndSet(Document document, Element parent, String childElement,
        String childValue) {/*from   w  w w  .j  a v a  2s . c  om*/
    Element child = (Element) document.createElement(childElement);
    parent.appendChild(child);
    child.setNodeValue(childValue);
    child.appendChild(document.createTextNode(childValue));
    return child;
}

From source file:Main.java

public static void createOptionalChildText(Document paramDocument, Element paramElement, String paramString1,
        String paramString2) {//from   ww w.j  a va 2s  .co  m
    if ((paramString2 == null) || (paramString2.length() == 0))
        return;
    Element localElement = paramDocument.createElement(paramString1);
    localElement.appendChild(paramDocument.createTextNode(paramString2));
    paramElement.appendChild(localElement);
}