Java XML Element Text Set setElementText(Element element, String text)

Here you can find the source of setElementText(Element element, String text)

Description

If the given Element has no text content, a new Text node added with the given text; otherwise, the first Text node will have its value changed to the given text.

License

Apache License

Parameter

Parameter Description
element The Element whose text content will be modified.
text The new text value.

Declaration

public static void setElementText(Element element, String text) 

Method Source Code

//package com.java2s;
/* //from ww  w . j a  v  a  2 s.c om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.Element;

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

public class Main {
    /**
     * 
     * If the given Element has no text content, a new Text node added with 
     * the given text; otherwise, the first Text node will have its value 
     * changed to the given text. If your Element has mixed content and you 
     * want to change the n-th Text Element, you're on your own.
     *
     * @param element
     *        The Element whose text content will be modified.
     * 
     * @param text
     *        The new text value.
     *
     */
    public static void setElementText(Element element, String text) {
        Node currentTextNode = null;

        NodeList children = element.getChildNodes();
        int length = children.getLength();

        //
        // first check to see if we already have text in the element
        //
        for (int n = 0; n < length && currentTextNode == null; ++n) {
            Node next = children.item(n);

            if (next.getNodeType() == Node.TEXT_NODE)
                currentTextNode = next;
        }

        //
        // if not, make a new text node and add it in
        //
        if (currentTextNode == null) {
            Document doc = element.getOwnerDocument();
            currentTextNode = doc.createTextNode("");
            currentTextNode = element.appendChild(currentTextNode);
        }

        //
        // once we definitely have a text node, (re-)set the value
        //
        currentTextNode.setNodeValue(text);
    }
}

Related

  1. setElementAttr(Element element, String attr, String attrVal)
  2. setElementContent(final Node n, final String s)
  3. setElementSimpleValue(Element element, String simpleValue)
  4. setElementsTextNodeValue(Node element, String value)
  5. setElementText(Element element, String value)
  6. setElementText(Element elm, String text)
  7. setElementText(Element elm, String text)
  8. setElementText(Element elm, String text)