Java XML Element Create createElementAndText(Element parent, String elementName, String text)

Here you can find the source of createElementAndText(Element parent, String elementName, String text)

Description

create Element And Text

License

GNU General Public License

Declaration

public static Element createElementAndText(Element parent,
            String elementName, String text) 

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 * Version: GPL 2.0/* w w  w.jav a2s.  c o  m*/
 *
 * The contents of this file are subject to the GNU General Public
 * License Version 2 or later (the "GPL").
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *   MiniG.org project members
 *
 * ***** END LICENSE BLOCK ***** */

import org.w3c.dom.Element;

import org.w3c.dom.Text;

public class Main {
    public static Element createElementAndText(Element parent,
            String elementName, String text) {
        if (text == null) {
            throw new NullPointerException("null text");
        }
        Element el = parent.getOwnerDocument().createElement(elementName);
        parent.appendChild(el);
        Text txt = el.getOwnerDocument().createTextNode(text);
        el.appendChild(txt);
        return el;
    }

    public static Element createElement(Element parent, String elementName) {
        Element el = parent.getOwnerDocument().createElement(elementName);
        parent.appendChild(el);
        return el;
    }
}

Related

  1. createElement(QName key)
  2. createElement(QName qName)
  3. createElement(String name, Object value, Document doc)
  4. createElement(String tag, String textContent, Document xml)
  5. createElement(String tagName, String text, Document doc)
  6. createElementInSameNamespace(Element parent, String localName)
  7. createElementInTempDocument(String name, String prefix, String namespaceURI)
  8. createElementLn(Document d, String name, String value, boolean isCDATA)
  9. createElementMapping(final Document doc1, final Document doc2)