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

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

Description

Set the value of the DOM node.

License

Apache License

Declaration

public static void setValueOfNode(Node node, String value) 

Method Source Code

//package com.java2s;
/*/* ww w. j  a  v  a  2  s .c  o  m*/
 * Copyright 1999-2005 The Apache Software Foundation.
 *
 * 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;

public class Main {
    /**
     * Set the value of the DOM node.
     * All current children of the node are removed and a new text node
     * with the value is appended.
     */
    public static void setValueOfNode(Node node, String value) {
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            node.setNodeValue(value);
        } else {
            while (node.hasChildNodes() == true) {
                node.removeChild(node.getFirstChild());
            }
            node.appendChild(node.getOwnerDocument().createTextNode(value));
        }
    }

    /**
     * Get the owner of the DOM document belonging to the node.
     * This works even if the node is the document itself.
     *
     * @param node The node.
     * @return     The corresponding document.
     */
    public static Document getOwnerDocument(Node node) {
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
            return (Document) node;
        } else {
            return node.getOwnerDocument();
        }
    }
}

Related

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