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;
/*//from  ww w .  j a v  a 2 s.  c  om
 *  soapUI, copyright (C) 2004-2010 eviware.com 
 *
 *  soapUI is free software; you can redistribute it and/or modify it under the 
 *  terms of version 2.1 of the GNU Lesser General Public License as published by 
 *  the Free Software Foundation.
 *
 *  soapUI 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 at gnu.org.
 */

import org.w3c.dom.CDATASection;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.ProcessingInstruction;
import org.w3c.dom.Text;

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());
        }
    }

    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. setElementContent(final Node n, final String s)
  2. setElementSimpleValue(Element element, String simpleValue)
  3. setElementsTextNodeValue(Node element, String value)
  4. setElementText(Element element, String text)
  5. setElementText(Element element, String value)
  6. setElementText(Element elm, String text)
  7. setElementText(Element elm, String text)
  8. setElementText(Node elem, Object text)
  9. setElementTextByAttr(Element modsroot, String nodename, String attrname, String attrvalue, String newValue)