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

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

Description

Sets the text for a node

License

Open Source License

Declaration

public synchronized static void setText(Node n, String text) 

Method Source Code


//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**//from www.j  a va 2s  .c  om
     *  Sets the text for a node
     */
    public synchronized static void setText(Node n, String text) {
        if (n == null)
            throw new IllegalArgumentException("Node argument cannot be null");
        if (text == null)
            throw new IllegalArgumentException("Node text argument cannot be null");

        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
                nl.item(i).setNodeValue(text);
                return;
            }
        }

        Node textNode = n.getOwnerDocument().createTextNode(text);
        n.appendChild(textNode);
    }
}

Related

  1. getValues(Node metric)
  2. nodeToDouble(Node node)
  3. putAll(final NamedNodeMap dst, final NamedNodeMap src)
  4. setText(Element parent, String text)
  5. setText(Element parentNode, String data)
  6. setText(Node node, String text)
  7. setText(Node node, String text)
  8. setText(Node node, String text)
  9. setText(Node node, String value)