Java XML Child Element Add appendElement(Element parent, Element child)

Here you can find the source of appendElement(Element parent, Element child)

Description

Appends another element as a child element.

License

Apache License

Parameter

Parameter Description
parent the parent element
child the child element to append

Declaration

public static void appendElement(Element parent, Element child) 

Method Source Code

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

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

public class Main {
    /**//  w w w  . ja  v a2 s  . c  o  m
     * Appends the child element to the parent element.
     *
     * @param parent
     *          the parent element
     * @param tagName
     *          the child element name
     * @return the child element added to the parent element
     */
    public static Element appendElement(Element parent, String tagName) {
        Element child = parent.getOwnerDocument().createElement(tagName);
        parent.appendChild(child);
        return child;
    }

    /**
     * Appends the child element as well as value to the parent element.
     *
     * @param parent
     *          the parent element
     * @param tagName
     *          the child element name
     * @param value
     *          the child element value
     * @return the child element added to the parent element
     */
    public static Element appendElement(Element parent, String tagName, String value) {
        Element child = appendElement(parent, tagName);
        child.setTextContent(value);
        return child;
    }

    /**
     * Appends another element as a child element.
     *
     * @param parent
     *          the parent element
     * @param child
     *          the child element to append
     */
    public static void appendElement(Element parent, Element child) {
        Node tmp = parent.getOwnerDocument().importNode(child, true);
        parent.appendChild(tmp);
    }
}

Related

  1. addChildNode(Node pNode, String name, String value)
  2. addChildren(Element element, List children)
  3. addElement(Element parent, Element child)
  4. addElement(Element parent, Element childElement)
  5. addElementChildTo(Element parent, String nsURI, String qualifiedName)
  6. appendElement(Element parent, Element child)
  7. appendTextNode(Node parent, String child)