Java XML Element Child Append appendChildElement(final Element parentElement, final String name, final String value)

Here you can find the source of appendChildElement(final Element parentElement, final String name, final String value)

Description

Appends a child element to the provided parent element.

License

LGPL

Parameter

Parameter Description
parentElement the parent element
name the name for the child element to be created
value the string value to be assigned to the created child element

Return

the newly created child element

Declaration

public static Element appendChildElement(final Element parentElement, final String name, final String value) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

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

public class Main {
    /**/*from ww  w  .j  a v a  2 s  . c o  m*/
     * Appends a child element to the provided parent element.
     * This child element is created with the name and integer value provided.
     *
     * @param parentElement the parent element
     * @param name the name for the child element to be created
     * @param value the integer value to be assigned to the created child element
     * @return the newly created child element 
     */
    public static Element appendChildElement(final Element parentElement, final String name, final int value) {
        return appendChildElement(parentElement, name, "" + value);
    }

    /**
     * Appends a child element to the provided parent element.
     * This child element is created with the name and boolean value provided.
     *
     * @param parentElement the parent element
     * @param name the name for the child element to be created
     * @param value the boolean value to be assigned to the created child element
     * @return the newly created child element
     */
    public static Element appendChildElement(final Element parentElement, final String name, final boolean value) {
        return appendChildElement(parentElement, name, value ? "true" : "false");
    }

    /**
     * Appends an empty child element to the provided parent element.
     * This child element is created with the name provided.
     *
     * @param parentElement the parent element
     * @param name the name for the child element to be created
     * @return the newly created empty child element 
     */
    public static Element appendChildElement(final Element parentElement, final String name) {
        return appendChildElement(parentElement, name, null);
    }

    /**
     * Appends a child element to the provided parent element.
     * This child element is created with the name and string value provided.
     *
     * @param parentElement the parent element
     * @param name the name for the child element to be created
     * @param value the string value to be assigned to the created child element
     * @return the newly created child element 
     */
    public static Element appendChildElement(final Element parentElement, final String name, final String value) {
        Document parentDocument = parentElement.getOwnerDocument();
        Element createElement = createElement(parentDocument, name);
        if (value != null) {
            createElement.appendChild(parentDocument.createTextNode(value));
        }
        parentElement.appendChild(createElement);
        return createElement;
    }

    private static Element createElement(final Document parentDocument, final String name) {
        return parentDocument.createElement(name);
    }
}

Related

  1. appendChild(Document doc, Node parentNode, String childName, String childContents)
  2. appendChild(Document document, Node root, String name, String value)
  3. appendChildElement(Document doc, Element parent, Element child)
  4. appendChildElement(Element parent, Element el, String[] order)
  5. appendChildElement(Element parentNode, String nodeName)
  6. appendChildElementBoolean(Element parentNode, String tagName, boolean content)
  7. appendChildElmt(Element aElmt, String aChildName)
  8. appendNewComment(Element parentElement, String commentText)
  9. appendNewElement(Document document, Element parent, Enum el)