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

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

Description

Add the child node to parent

License

Apache License

Parameter

Parameter Description
parent a parameter
child a parameter

Declaration

public static void addElement(Element parent, Element child) 

Method Source Code


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

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

public class Main {
    /**//w  w w.  ja v a 2s  . c o  m
     * Add the child node to parent
     *
     * @param parent
     * @param child
     */
    public static void addElement(Element parent, Element child) {
        parent.appendChild(child);
    }

    /**
     * Add the element to the parent by the node name
     *
     * @param parent
     * @param tagName
     */
    public static void addElement(Element parent, String tagName) {
        Document doc = parent.getOwnerDocument();
        Element child = doc.createElement(tagName);
        parent.appendChild(child);
    }

    /**
     * add element to parent withe the specified value by the node name
     *
     * @param parent
     * @param tagName
     * @param text
     */
    public static void addElement(Element parent, String tagName, String text) {
        Document doc = parent.getOwnerDocument();
        Element child = doc.createElement(tagName);
        setElementValue(child, text);
        parent.appendChild(child);
    }

    /**
     * Create an element with the specified name
     *
     * @param parent
     * @param tagName
     * @return
     */
    public static Element createElement(Element parent, String tagName) {
        Document doc = parent.getOwnerDocument();
        Element child = doc.createElement(tagName);
        parent.appendChild(child);
        return child;
    }

    /**
     * create an child element with the specified name and value and append it in a parent element
     *
     * @param parent
     * @param tagName
     * @param value
     * @return
     */
    public static Element createElement(Element parent, String tagName, String value) {
        Document doc = parent.getOwnerDocument();
        Element child = doc.createElement(tagName);
        setElementValue(child, value);
        parent.appendChild(child);
        return child;
    }

    /**
     * Set the value of element
     *
     * @param element
     * @param val
     */
    public static void setElementValue(Element element, String val) {
        Node node = element.getOwnerDocument().createTextNode(val);
        NodeList nl = element.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node nd = nl.item(i);
            if (nd.getNodeType() == Node.TEXT_NODE) {
                nd.setNodeValue(val);
                return;
            }
        }
        element.appendChild(node);
    }
}

Related

  1. addChildElement(Node parent, String elementName, String textContent)
  2. addChildElement(Node parent, String tag, String value)
  3. addChildElements(Node n, List elements)
  4. addChildNode(Node pNode, String name, String value)
  5. addChildren(Element element, List children)
  6. addElement(Element parent, Element childElement)
  7. addElementChildTo(Element parent, String nsURI, String qualifiedName)
  8. appendElement(Element parent, Element child)
  9. appendElement(Element parent, Element child)