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

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

Description

set Element Text

License

Open Source License

Declaration

static public void setElementText(Element elm, String text) 

Method Source Code

//package com.java2s;
/**// ww  w.j a v a  2s .c  o  m
 * Copyright (c) 2012 centeractive ag. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */

import org.w3c.dom.*;

public class Main {
    static public void setElementText(Element elm, String text) {
        Node node = elm.getFirstChild();
        if (node == null) {
            if (text != null)
                elm.appendChild(elm.getOwnerDocument().createTextNode(text));
        } else if (node.getNodeType() == Node.TEXT_NODE) {
            if (text == null)
                node.getParentNode().removeChild(node);
            else
                node.setNodeValue(text);
        } else if (text != null) {
            Text textNode = node.getOwnerDocument().createTextNode(text);
            elm.insertBefore(textNode, elm.getFirstChild());
        }
    }

    /**
     * Gets the index of the specified element amongst elements with the same
     * name
     *

     * @return the index of the element, will be >= 1
     */

    public static boolean setNodeValue(Node domNode, String string) {
        if (domNode == null)
            return false;

        short nodeType = domNode.getNodeType();

        switch (nodeType) {
        case Node.ELEMENT_NODE: {
            setElementText((Element) domNode, string);
            break;
        }
        case Node.ATTRIBUTE_NODE:
        case Node.TEXT_NODE: {
            domNode.setNodeValue(string);
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            ((ProcessingInstruction) domNode).setData(string);
            break;
        }
        case Node.CDATA_SECTION_NODE: {
            ((CDATASection) domNode).setData(string);
            break;
        }
        default: {
            return false;
        }
        }

        return true;
    }
}

Related

  1. setElementSimpleValue(Element element, String simpleValue)
  2. setElementsTextNodeValue(Node element, String value)
  3. setElementText(Element element, String text)
  4. setElementText(Element element, String value)
  5. setElementText(Element elm, String text)
  6. setElementText(Element elm, String text)
  7. setElementText(Node elem, Object text)
  8. setElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue, String newValue)
  9. setElementTextValue(Element e, String text)