Java XML Node Value setTextValue(Node node, String value)

Here you can find the source of setTextValue(Node node, String value)

Description

Set the text associated with a node.

License

Open Source License

Return

the text associated to the node

Declaration

public static void setTextValue(Node node, String value) 

Method Source Code

//package com.java2s;
/*//from www. ja  va  2s . c  o m
 * ? Copyright IBM Corp. 2012
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.Text;

public class Main {
    /**
     * Set the text associated with a node.
     * If case of an Element, it finds the first occurence of text and change its content. If 
     * none is available, then it add a new one at the end of the content. For the other nodes, 
     * it then calls setNodeValue()
     * @return the text associated to the node
     */
    public static void setTextValue(Node node, String value) {
        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) {
                        ((Text) child).setNodeValue(value);
                        return;
                    }
                    child = child.getNextSibling();
                    if (child == null) {
                        break;
                    }
                }
                if (node.hasChildNodes()) {
                    removeChildren(node);
                }
                Text textNode = node.getOwnerDocument().createTextNode(value);
                node.appendChild(textNode);
            } else {
                node.setNodeValue(value);
            }
        }
    }

    public static void setTextValue(Node node, String value, boolean cdata) {
        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 && !cdata) {
                        ((Text) child).setNodeValue(value);
                        return;
                    }
                    if (child.getNodeType() == Node.CDATA_SECTION_NODE && cdata) {
                        ((Text) child).setNodeValue(value);
                        return;
                    }
                }
                if (node.hasChildNodes()) {
                    removeChildren(node);
                }
                Node textNode = cdata ? node.getOwnerDocument().createCDATASection(value)
                        : node.getOwnerDocument().createTextNode(value);
                node.appendChild(textNode);
            } else {
                node.setNodeValue(value);
            }
        }
    }

    /**
     * Remove all the children from a node.
     * This method is surprisingly absent from the Node interface.
     * @param node the node to remove the children 
     */
    public static void removeChildren(Node node) {
        if (node != null) {
            while (node.hasChildNodes()) {
                node.removeChild(node.getFirstChild());
            }
        }
    }

    /**
     * Return the owner document for the specified node.
     * 
     * @param node
     * @return
     */
    public static Document getOwnerDocument(Node node) {
        if (node instanceof Document) {
            return (Document) node;
        }
        return node.getOwnerDocument();
    }
}

Related

  1. setText(Node node, String value)
  2. setTextContent(Node namespaceNode, String textContent)
  3. setTextContent(Node node, final String text)
  4. setTextValue(Node aNode, String aValue)
  5. setTextValue(Node node, String newValue)
  6. setValueOfNode(Node node, String value)
  7. xmlGetFirstTextValue(Node node)
  8. xmlGetText(Node node)
  9. xmlNodeGetValue(Node node)