Example usage for org.w3c.dom Text setData

List of usage examples for org.w3c.dom Text setData

Introduction

In this page you can find the example usage for org.w3c.dom Text setData.

Prototype

public void setData(String data) throws DOMException;

Source Link

Document

The character data of the node that implements this interface.

Usage

From source file:Main.java

public static Element addTextNode(Document xmlDoc, Element ndParent, String nodeName, String nodeValue) {

    Element ndNode = xmlDoc.createElement(nodeName);
    Text ndTextNode = xmlDoc.createTextNode(nodeName);
    ndTextNode.setData(nodeValue);
    ndNode.appendChild(ndTextNode);//  w w w .  ja  v a  2 s.co m
    ndParent.appendChild(ndNode);
    return ndNode;

}

From source file:Main.java

License:asdf

public static void newEmail(Document doc, String newname, String newemail) {
    Element root = doc.getDocumentElement();
    NodeList rootlist = root.getChildNodes();
    for (int i = 0; i < rootlist.getLength(); i++) {
        Element person = (Element) rootlist.item(i);
        NodeList personlist = person.getChildNodes();
        Element name = (Element) personlist.item(0);
        NodeList namelist = name.getChildNodes();
        Text nametext = (Text) namelist.item(0);
        String oldname = nametext.getData();
        if (oldname.equals(newname)) {
            Element email = (Element) personlist.item(1);
            NodeList emaillist = email.getChildNodes();
            Text emailtext = (Text) emaillist.item(0);
            emailtext.setData(newemail);
        }//from   www . j av  a 2 s.  co m
    }
}

From source file:Main.java

/**
 * replace the text value in the target//w  w  w. j a v a2  s.c  o  m
 *
 * @param target
 * @param value
 * @return
 */
public static boolean replaceTextValue(final Element target, final String value) {
    if (target == null) {
        return false;
    }
    final NodeList nodeList = target.getChildNodes();
    if (nodeList == null) {
        return false;
    }
    for (int current = 0; current < nodeList.getLength(); current++) {
        final Node node = nodeList.item(current);
        if (node instanceof Text) {
            final Text text = (Text) node;
            text.setData(value);
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static boolean updateTextNode(String nodeName, Element searchFrom, String data, int position)
        throws Exception {
    boolean result = false;
    Element currentElement = (Element) searchFrom.getElementsByTagName(nodeName).item(position);

    if (currentElement != null && currentElement.getNodeType() == Node.ELEMENT_NODE) {
        Text textNode = (Text) (currentElement.getFirstChild());
        textNode.setData(data);

        if (textNode != null && textNode.getNodeValue() == null)
            result = false;// ww  w.  j ava 2s  . c o m
        else
            result = true;
    }
    return result;
}

From source file:Main.java

/**
 * Set the text of the specified element to the given string.
 * /*  w  ww  .  j a  v  a2 s  . c  om*/
 * @param e The element.
 * @param text The text string.
 */
public static void setText(Element e, String text) {
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            t.setData(text.trim());
            return;
        }
    }
    Document doc = e.getOwnerDocument();
    // bit of a hack - we preserve the cdata on the way in so we can serialize correctly
    // This only works on xml to xml 
    // TODO need to have a "preserve format" or some such on the mdmi structure
    if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
        CDATASection cdata = doc
                .createCDATASection(text != null ? text.substring(9, text.lastIndexOf("]]>")) : null);
        e.appendChild(cdata);
    } else {
        Text txt = doc.createTextNode(text != null ? text.trim() : null);
        e.appendChild(txt);
    }

}

From source file:Main.java

public static void setNodeValue(Node node, String name, String value) {
    String s = node.getNodeValue();
    if (s != null) {
        node.setNodeValue(value);/*  w  w w. j  av  a 2 s .  c  o m*/
        return;
    }
    NodeList nodelist = node.getChildNodes();
    for (int i = 0; i < nodelist.getLength(); i++) {
        if (nodelist.item(i) instanceof Text) {
            Text text = (Text) nodelist.item(i);
            text.setData(value);
            return;
        }
    }
    return;
}

From source file:Main.java

public static void modifyTextByReplacement(Document doc) {
    Element root = doc.getDocumentElement();
    Element place = (Element) root.getFirstChild();
    Text name = (Text) place.getFirstChild().getFirstChild();
    Text directions = (Text) place.getLastChild().getFirstChild();

    name.setData("newName");
    directions.setData("newDirection");
}

From source file:Main.java

/**
 * Sets element TEXT data/*from ww w  .ja  va  2 s  .c  o m*/
 * 
 * @param e
 *            the element
 * @param data
 *            the new data
 */
public static void setElementTextValue(Element e, String data) {
    Text txt = getElementTextNode(e);
    if (txt != null) {
        txt.setData(data);
    } else {
        txt = e.getOwnerDocument().createTextNode(data);
        e.appendChild(txt);
    }
}

From source file:Main.java

/**
 * Sets data to be the TEXT content of element
 *
 * @param parentNode the parent element.
 * @param data the data to set.//ww w. jav a  2 s .c om
 */
public static void setText(Element parentNode, String data) {
    if (data == null)
        return;

    Text txt = getTextNode(parentNode);

    if (txt != null)
        txt.setData(data);
    else {
        txt = parentNode.getOwnerDocument().createTextNode(data);
        parentNode.appendChild(txt);
    }
}

From source file:DOMEdit.java

public static void newEmail(Document doc, String newname, String newemail) {
        Element root = doc.getDocumentElement();
        Element person = (Element) root.getFirstChild();
        while (person != null) {
            Element name = (Element) person.getFirstChild();
            Text nametext = (Text) name.getFirstChild();
            String oldname = nametext.getData();
            if (oldname.equals(newname)) {
                Element email = (Element) name.getNextSibling();
                Text emailtext = (Text) email.getFirstChild();
                emailtext.setData(newemail);
            }/*from  w ww .  ja v  a 2 s.com*/
            person = (Element) person.getNextSibling();
        }
    }