Java XML Child Element Add addChildElement(Node parent, String tag, String value)

Here you can find the source of addChildElement(Node parent, String tag, String value)

Description

Adds a Child element to the parent node.

License

Apache License

Parameter

Parameter Description
parent The parent node
tag The child element name
value Value of text node appended to the Child

Return

the newly added Child node.

Declaration

public static Element addChildElement(Node parent, String tag,
        String value) 

Method Source Code

//package com.java2s;
/*/*from  w w  w .  j  a v a2s  .  c  o  m*/
 * $Id$ 
 *
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * 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.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * Adds a Child element to the parent node. If the 'value' is
     * not null, a Text node with the same value is appended to the
     * Child.
     * @param parent The parent node
     * @param tag The child element name
     * @param value Value of text node appended to the Child
     * @return the newly added Child node.
     */
    public static Element addChildElement(Node parent, String tag,
            String value) {
        Document doc = (parent.getNodeType() == Node.DOCUMENT_NODE) ? (Document) parent
                : parent.getOwnerDocument();
        Element child = doc.createElement(tag);
        if (value != null)
            child.appendChild(doc.createTextNode(value));
        parent.appendChild(child);
        return child;
    }
}

Related

  1. addChildElement(Element parentElement, String elementName)
  2. addChildElement(Element parentElement, String elementName)
  3. addChildElement(Element root, Element element, String name)
  4. addChildElement(Node element, String name, String[][] attributes)
  5. addChildElement(Node parent, String elementName, String textContent)
  6. addChildElements(Node n, List elements)
  7. addChildNode(Node pNode, String name, String value)
  8. addChildren(Element element, List children)
  9. addElement(Element parent, Element child)