Creates a sub XML element with a single child, a text node, as a child of an existing node. - Java XML

Java examples for XML:XML Node Child

Description

Creates a sub XML element with a single child, a text node, as a child of an existing node.

Demo Code

/*// w  w  w .  j a  v a 2 s .c om
 *  Copyright 2006-2015 WebPKI.org (http://webpki.org).
 *
 *  Licensed 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.
 *
 */
//package com.java2s;

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

public class Main {
    /**
     * Creates a subelement with a single child, a text node, as a child of an existing node.
     * <p>The structure <code>&lt;<i>element</i>&gt;<i>value</i>&lt;/<i>element</i>&gt;</code>
     * will be created and appended to <i>parent</i>.
     * @param parent The parent of the new element.
     * @param element The name of the new element.
     * @param value The value of the text node.
     * @return Element
     */
    public static Element appendTextElement(Element parent, String element,
            String value) {
        Document d = parent.getOwnerDocument();
        Element r = d.createElement(element);
        parent.appendChild(r);
        r.appendChild(d.createTextNode(value));

        return r;
    }

    /**
     * Creates a subelement with a single child, a text node, as a child of an existing node.
     * <p>The structure <code>&lt;<i>element</i>&gt;<i>value</i>&lt;/<i>element</i>&gt;</code>
     * will be created and appended to <i>parent</i>.
     * <p>This method is shorthand for 
     * <code>{@link #appendTextElement(Element, String, String) appendTextElement}(parent, element, Integer.toString(value))</code>
     * @param parent The parent of the new element.
     * @param element The name of the new element.
     * @param value The value of the text node.
     * @return Element
     * @see #appendTextElement(Element, String, String)
     */
    public static Element appendTextElement(Element parent, String element,
            int value) {
        return appendTextElement(parent, element, Integer.toString(value));
    }

    /**
     * Creates a subelement with a single child, a text node, as a child of an existing node.
     * <p>The structure <code>&lt;<i>element</i>&gt;<i>value</i>&lt;/<i>element</i>&gt;</code>
     * will be created and appended to <i>parent</i>.
     * <p>This method is shorthand for 
     * <code>{@link #appendTextElement(Element, String, String) appendTextElement}(parent, element, Long.toString(value))</code>
     * @param parent The parent of the new element.
     * @param element The name of the new element.
     * @param value The value of the text node.
     * @return Element
     * @see #appendTextElement(Element, String, String)
     */
    public static Element appendTextElement(Element parent, String element,
            long value) {
        return appendTextElement(parent, element, Long.toString(value));
    }

    /**
     * Creates a subelement with a single child, a text node, as a child of an existing node.
     * <p>The structure <code>&lt;<i>element</i>&gt;<i>value</i>&lt;/<i>element</i>&gt;</code>
     * will be created and appended to <i>parent</i>.
     * <p>This method is shorthand for 
     * <code>{@link #appendTextElement(Element, String, String) appendTextElement}(parent, element, value.toString())</code>
     * and is hence useful when value is an object who's {@link Object#toString toString} function returns a suitably formatted
     * string representation, for example a {@link java.lang.StringBuffer StringBuffer},
     * {@link java.math.BigInteger BigInteger} or {@link java.math.BigDecimal BigDecimal}.
     * @param parent The parent of the new element.
     * @param element The name of the new element.
     * @param value The value of the text node.
     * @return Element
     * @see #appendTextElement(Element, String, String)
     */
    public static Element appendTextElement(Element parent, String element,
            Object value) {
        return appendTextElement(parent, element, value.toString());
    }
}

Related Tutorials