Java XML Node Value setText(Node node, String text)

Here you can find the source of setText(Node node, String text)

Description

Sets text for a node.

License

Open Source License

Parameter

Parameter Description
node Node.
text New text for the node.

Declaration


public static void setText(Node node, String text) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

import org.w3c.dom.*;

public class Main {
    /**   Sets text for a node.
     */*from  ww w . j a v  a2 s . c o  m*/
     *   <p>Sets the value of the first child text node, if any.
     *   Creates new child text node if none found.</p>
     *
     *   @param   node      Node.
     *
     *   @param   text      New text for the node.
     */

    public static void setText(Node node, String text) {
        NodeList children = node.getChildNodes();
        int numChildren = children.getLength();
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.TEXT_NODE) {
                child.setNodeValue(text);
                return;
            }
        }
        Text child = node.getOwnerDocument().createTextNode(text);
        node.appendChild(child);
    }
}

Related

  1. setText(Element parent, String text)
  2. setText(Element parentNode, String data)
  3. setText(Node n, String text)
  4. setText(Node node, String text)
  5. setText(Node node, String text)
  6. setText(Node node, String value)
  7. setTextContent(Node namespaceNode, String textContent)
  8. setTextContent(Node node, final String text)
  9. setTextValue(Node aNode, String aValue)