Java XML Element Text Set setElementValue(Element element, String val)

Here you can find the source of setElementValue(Element element, String val)

Description

Set the value of element

License

Apache License

Parameter

Parameter Description
element a parameter
val a parameter

Declaration

public static void setElementValue(Element element, String val) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*from   w w w  .j  ava2  s. com*/
     * Set the value of element
     *
     * @param element
     * @param val
     */
    public static void setElementValue(Element element, String val) {
        Node node = element.getOwnerDocument().createTextNode(val);
        NodeList nl = element.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node nd = nl.item(i);
            if (nd.getNodeType() == Node.TEXT_NODE) {
                nd.setNodeValue(val);
                return;
            }
        }
        element.appendChild(node);
    }
}

Related

  1. setElementText(Element elm, String text)
  2. setElementText(Element elm, String text)
  3. setElementText(Node elem, Object text)
  4. setElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue, String newValue)
  5. setElementTextValue(Element e, String text)
  6. setElementValue(Element element, String value)