Java XML Duration Add appendTextElement(Element parent, String element, long value)

Here you can find the source of appendTextElement(Element parent, String element, long value)

Description

Creates a subelement with a single child, a text node, as a child of an existing node.

License

Apache License

Parameter

Parameter Description
parent The parent of the new element.
element The name of the new element.
value The value of the text node.

Return

Element

Declaration

public static Element appendTextElement(Element parent, String element, long value) 

Method Source Code

//package com.java2s;
/*//ww  w  .  j a  v  a  2 s .co  m
 *  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.
 *
 */

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

  1. appendElement(Node parent, String name)
  2. appendElement(Node parent, String tagName)
  3. appendElementNS(Element parent, String namespaceURI, String localName)
  4. appendStringElement(Element parentElement, String nodeName, String nodeValue)
  5. appendText(Element parent, String content)
  6. appendTextElement(Element parent, String name, String content)
  7. appendTextNode(Element parentElement, String initialTextContent)