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

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

Description

Appends the specified value as a text node to the element.

License

BSD License

Parameter

Parameter Description
element the element.
value the element's value.

Declaration

public static void setElementValue(Element element, String value) 

Method Source Code


//package com.java2s;
/*/*from w  ww  . j  ava 2s.c o m*/
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import org.w3c.dom.*;

public class Main {
    /**
     * Appends the specified value as a text node to the element. If the
     * value is <code>null</code>, the element's first child node will be
     * removed.
     *
     * @param element the element.
     * @param value   the element's value.
     */
    public static void setElementValue(Element element, String value) {
        Node child = element.getFirstChild();

        if (value != null) {
            if (child == null) {
                child = element.getOwnerDocument().createTextNode("");
                element.appendChild(child);
            }

            child.setNodeValue(value);
        } else {
            if (child != null) {
                element.removeChild(child);
            }
        }
    }
}

Related

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