Java XML Child Element Create createTextChild(Node elem, String elemName, Boolean text)

Here you can find the source of createTextChild(Node elem, String elemName, Boolean text)

Description

create Text Child

License

Open Source License

Declaration

public static Element createTextChild(Node elem, String elemName, Boolean text) 

Method Source Code

//package com.java2s;
/*/* w  w  w.  j  a  v a2s  . c om*/
 * This file is part of ROOSSTER.
 * Copyright 2004, Benjamin Reitzammer <benjamin@roosster.org>
 * All rights reserved.
 *
 * ROOSSTER is free software; you can redistribute it and/or modify
 * it under the terms of the Artistic License.
 *
 * You should have received a copy of the Artistic License
 * along with ROOSSTER; if not, go to
 * http://www.opensource.org/licenses/artistic-license.php for details
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * Same as {@link createChild(Node, String) createChild()}, but adds the
     * specified <code>text</code> to the newly created <code>Node</code>.
     * 
     * @see #createChild(Node, String)
     * @param elem the <code>Node</code> to which the newly created 
     * <code>Node</code> will be appended to. Not allowed to be <code>null</code>.
     * @param elemName the name of the to-be-created <code>Node</code>, not allowed 
     * to be empty or <code>null</code>.
     * @param text the text-contents of the created/inserted node  
     * @return the created element
     * @exception IllegalArgumentException if <code>elem</code> and/ord
     * <code>elemName</code> are null (or empty in the case of <code>elemName</code>)
     */
    public static Element createTextChild(Node node, String elemName, String text) {
        if (node == null || elemName == null || "".equals(elemName))
            throw new IllegalArgumentException("Arguments are not allowed to be null or empty");

        Document document = null;

        if (node instanceof Document) {
            document = (Document) node;
        } else if (node.getOwnerDocument() != null) {
            document = node.getOwnerDocument();
        }

        Element newChild = null;
        if (document != null) {
            newChild = document.createElement(elemName);
            node.appendChild(newChild);
            newChild.appendChild(document.createTextNode(text));
        }

        return newChild;
    }

    /**
     * @see #createChild(Element, String, String)
     */
    public static Element createTextChild(Node elem, String elemName, Integer text) {
        return createTextChild(elem, elemName, String.valueOf(text));
    }

    /**
     * @see #createChild(Element, String, String)
     */
    public static Element createTextChild(Node elem, String elemName, Float text) {
        return createTextChild(elem, elemName, String.valueOf(text));
    }

    /**
     * @see #createChild(Element, String, String)
     */
    public static Element createTextChild(Node elem, String elemName, Boolean text) {
        return createTextChild(elem, elemName, text == null ? "false" : text.toString());
    }
}

Related

  1. createChildElement(Element parent, String name)
  2. createChildElement(Element parentElement, String elementName)
  3. createChildElement(Element parentElement, String strTagName)
  4. createChildElement(final String tagName, final Node parent, final Document document)
  5. createChildElement(String tagName, Element parentElement)
  6. getChildElementByChain(Element element, String[] chain, boolean create)
  7. getOrCreateChildNode(IIOMetadataNode parentNode, String name)
  8. getOrCreateFirstTextChild(Element node)
  9. hasActivityChildNodeWithCreateInstanceSet(Node node)