Java Utililty Methods XML Element Text Set

List of utility methods to do XML Element Text Set

Description

The list of methods to do XML Element Text Set are organized into topic(s).

Method

voidsetElementAttr(Element element, String attr, String attrVal)
Set the value of attribute
element.setAttribute(attr, attrVal);
voidsetElementContent(final Node n, final String s)
Replace the content for the current element.
NodeList children = n.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node curnode = children.item(i);
    n.removeChild(curnode);
Document d = n.getOwnerDocument();
final Node textNode = d.createTextNode(s);
n.appendChild(textNode);
...
voidsetElementSimpleValue(Element element, String simpleValue)
In WTP, element values are text nodes that are child of the element.
Text textNode = element.getOwnerDocument().createTextNode(simpleValue);
NodeList children = element.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
    element.removeChild(children.item(i));
element.appendChild(textNode);
voidsetElementsTextNodeValue(Node element, String value)
set Elements Text Node 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);
for (Iterator<Node> iter = childs_to_remove.iterator(); iter.hasNext();)
...
voidsetElementText(Element element, String text)
If the given Element has no text content, a new Text node added with the given text; otherwise, the first Text node will have its value changed to the given text.
Node currentTextNode = null;
NodeList children = element.getChildNodes();
int length = children.getLength();
for (int n = 0; n < length && currentTextNode == null; ++n) {
    Node next = children.item(n);
    if (next.getNodeType() == Node.TEXT_NODE)
        currentTextNode = next;
if (currentTextNode == null) {
    Document doc = element.getOwnerDocument();
    currentTextNode = doc.createTextNode("");
    currentTextNode = element.appendChild(currentTextNode);
currentTextNode.setNodeValue(text);
voidsetElementText(Element element, String value)
Sets the text content of a DOM Element.
NodeList children = element.getChildNodes();
int childCount = children.getLength();
for (int index = 0; index < childCount; ++index) {
    if (children.item(index) instanceof Text) {
        Text text = (Text) children.item(index);
        text.setData(value);
        return;
Text text = element.getOwnerDocument().createTextNode(value);
element.appendChild(text);
voidsetElementText(Element elm, String text)
set Element Text
Node node = elm.getFirstChild();
if (node == null) {
    if (text != null)
        elm.appendChild(elm.getOwnerDocument().createTextNode(text));
} else if (node.getNodeType() == Node.TEXT_NODE) {
    if (text == null)
        node.getParentNode().removeChild(node);
    else
...
voidsetElementText(Element elm, String text)
set Element Text
Node node = elm.getFirstChild();
if (node == null) {
    if (text != null)
        elm.appendChild(elm.getOwnerDocument().createTextNode(text));
} else if (node.getNodeType() == Node.TEXT_NODE) {
    if (text == null)
        node.getParentNode().removeChild(node);
    else
...
voidsetElementText(Element elm, String text)
set Element Text
Node node = elm.getFirstChild();
if (node == null) {
    if (text != null) {
        elm.appendChild(elm.getOwnerDocument().createTextNode(text));
} else if (node.getNodeType() == Node.TEXT_NODE) {
    if (text == null) {
        node.getParentNode().removeChild(node);
...
booleansetElementText(Node elem, Object text)
Sets the text value of an Element.
if (elem == null)
    return false; 
Node node = elem.getFirstChild();
while (node != null) { 
    if (node.getNodeType() == Node.TEXT_NODE)
        break; 
    node = node.getNextSibling();
if (node != null) { 
    if (text != null)
        node.setNodeValue(text.toString());
    else
        elem.removeChild(node);
} else if (text != null) { 
    elem.appendChild(elem.getOwnerDocument().createTextNode(text.toString()));
return true;