Java XML Node Value setText(Node node, String text)

Here you can find the source of setText(Node node, String text)

Description

Creates a Text child, or replaces all existing children of type Text.

License

Open Source License

Return

The Text child which receives the specified text.

Declaration

public static Text setText(Node node, String text) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2006-2013 AITIA International, Inc.
 * /*from w ww.j  a v a  2 s .c o  m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

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

public class Main {
    /**
     * Creates a Text child, or replaces all existing children of type Text.
     * Non-Text descendants are preserved without change.
     * @return The Text child which receives the specified text. 
     * @see Node.setTextContent()
     */
    public static Text setText(Node node, String text) {
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0, n = children.getLength(); i < n; ++i) {
                Node child = children.item(i);
                if (child instanceof Text) {
                    return ((Text) child).replaceWholeText(text);
                }
            }
        }
        Text ans = node.getOwnerDocument().createTextNode(text);
        node.appendChild(ans);
        return ans;
    }
}

Related

  1. nodeToDouble(Node node)
  2. putAll(final NamedNodeMap dst, final NamedNodeMap src)
  3. setText(Element parent, String text)
  4. setText(Element parentNode, String data)
  5. setText(Node n, String text)
  6. setText(Node node, String text)
  7. setText(Node node, String text)
  8. setText(Node node, String value)
  9. setTextContent(Node namespaceNode, String textContent)