Java Utililty Methods XML Node Value

List of utility methods to do XML Node Value

Description

The list of methods to do XML Node Value are organized into topic(s).

Method

voidsetText(Node node, String text)
Sets text for a node.
NodeList children = node.getChildNodes();
int numChildren = children.getLength();
for (int i = 0; i < numChildren; i++) {
    Node child = children.item(i);
    if (child.getNodeType() == Node.TEXT_NODE) {
        child.setNodeValue(text);
        return;
Text child = node.getOwnerDocument().createTextNode(text);
node.appendChild(child);
voidsetText(Node node, String value)
Set the text of a specified node.
if (node == null)
    return;
Node child = findChild(node, "#text");
if (child == null)
    node.appendChild(node.getOwnerDocument().createTextNode(value));
else
    child.setNodeValue(value);
TextsetTextContent(Node namespaceNode, String textContent)
set Text Content
Text retval = null;
if (namespaceNode instanceof Text) {
    namespaceNode.setNodeValue(textContent);
    retval = (Text) namespaceNode;
} else if (namespaceNode instanceof Element) {
    Element namespaceElement = (Element) namespaceNode;
    removeChildren(namespaceElement);
    retval = namespaceElement.getOwnerDocument().createTextNode(textContent);
...
voidsetTextContent(Node node, final String text)
based on public Java5 javadoc of org.w3c.dom.Node.setTextContent method
while (node.hasChildNodes()) {
    node.removeChild(node.getFirstChild());
if (text != null && text.length() > 0) {
    Node textNode = node.getOwnerDocument().createTextNode(text);
    node.appendChild(textNode);
voidsetTextValue(Node aNode, String aValue)
set Text Value
Document wDocument = aNode.getOwnerDocument();
Text wText = wDocument.createTextNode(aValue);
aNode.appendChild(wText);
voidsetTextValue(Node node, String newValue)
set the text node from an element node.
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    if (list.item(i).getNodeType() == Node.TEXT_NODE) {
        list.item(i).setNodeValue(newValue);
voidsetTextValue(Node node, String value)
Set the text associated with a node.
if (node != null) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child.getNodeType() == Node.TEXT_NODE) {
                ((Text) child).setNodeValue(value);
                return;
            if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
...
voidsetValueOfNode(Node node, String value)
Set the value of the DOM node.
if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
    node.setNodeValue(value);
} else {
    while (node.hasChildNodes() == true) {
        node.removeChild(node.getFirstChild());
    node.appendChild(node.getOwnerDocument().createTextNode(value));
StringxmlGetFirstTextValue(Node node)
Metode til at læse værdien af den første child tekst node i en DOM Node.
String theValue = "";
NodeList theNodeList = node.getChildNodes();
if (theNodeList != null) {
    for (int i = 0; i < theNodeList.getLength(); i++) {
        Node textNode = theNodeList.item(i);
        if (textNode != null) {
            theValue = textNode.getNodeValue();
            if (theValue == null) {
...
StringxmlGetText(Node node)
Returns the text of the node.
NodeList kids = node.getChildNodes();
for (int i = 0; i < kids.getLength(); i++) {
    Node kid = kids.item(i);
    if (kid.getNodeName().equals("#text"))
        return kid.getNodeValue();
    else if (kid.getNodeName().equals("#cdata-section"))
        return kid.getNodeValue();
return null;