Java XML Element Text Set setElementsTextNodeValue(Node element, String value)

Here you can find the source of setElementsTextNodeValue(Node element, String value)

Description

set Elements Text Node Value

License

Open Source License

Declaration

public static void setElementsTextNodeValue(Node element, String value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.Iterator;
import java.util.List;

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

public class Main {
    public static void setElementsTextNodeValue(Node element, String value) {

        NodeList children = element.getChildNodes();
        List<Node> childs_to_remove = new ArrayList<Node>();

        for (int i = 0; i < children.getLength(); i++) {

            Node child = children.item(i);

            if (child != null && child.getNodeType() == Node.TEXT_NODE)
                childs_to_remove.add(child);
        }/*from   w  w  w  .j  ava2  s.c o  m*/

        for (Iterator<Node> iter = childs_to_remove.iterator(); iter.hasNext();)
            element.removeChild(iter.next());

        Node text_node = element.getOwnerDocument().createTextNode(value);
        element.appendChild(text_node);
    }
}

Related

  1. setElementAttr(Element element, String attr, String attrVal)
  2. setElementContent(final Node n, final String s)
  3. setElementSimpleValue(Element element, String simpleValue)
  4. setElementText(Element element, String text)
  5. setElementText(Element element, String value)
  6. setElementText(Element elm, String text)
  7. setElementText(Element elm, String text)