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

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

Description

Set the text of a specified node.

License

Open Source License

Declaration

static public void setText(Node node, String value) 

Method Source Code

//package com.java2s;
/*/*  www.jav a 2s.  c  o  m*/
*   EuroCarbDB, a framework for carbohydrate bioinformatics
*
*   Copyright (c) 2006-2009, Eurocarb project, or third-party contributors as
*   indicated by the @author tags or express copyright attribution
*   statements applied by the authors.  
*
*   This copyrighted material is made available to anyone wishing to use, modify,
*   copy, or redistribute it subject to the terms and conditions of the GNU
*   Lesser General Public License, as published by the Free Software Foundation.
*   A copy of this license accompanies this distribution in the file LICENSE.txt.
*
*   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 Lesser General Public License
*   for more details.
*
*   Last commit: $Rev: 1210 $ by $Author: glycoslave $ on $Date:: 2009-06-12 #$  
*/

import org.w3c.dom.*;

public class Main {
    /**
       Set the text of a specified node. A text section is added to
       the node if necessary.
     */
    static public void setText(Node node, String value) {
        if (node == null)
            return;

        Node child = findChild(node, "#text");
        if (child == null)
            node.appendChild(node.getOwnerDocument().createTextNode(value));
        else
            child.setNodeValue(value);
    }

    /**
       Return the first children of a node with a given name.
       @return <code>null</code> if no child is found
     */
    static public Node findChild(Node node, String child_name) {
        if (node == null)
            return null;

        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeName().equals(child_name))
                return child;
        }
        return null;
    }
}

Related

  1. setText(Element parentNode, String data)
  2. setText(Node n, String text)
  3. setText(Node node, String text)
  4. setText(Node node, String text)
  5. setText(Node node, String text)
  6. setTextContent(Node namespaceNode, String textContent)
  7. setTextContent(Node node, final String text)
  8. setTextValue(Node aNode, String aValue)
  9. setTextValue(Node node, String newValue)