Java XML Child Element Create createChildElement(Document doc, Element parent, String name)

Here you can find the source of createChildElement(Document doc, Element parent, String name)

Description

This method is used to create a Child element.

License

Apache License

Parameter

Parameter Description
doc document on which the Child Element has to be created.
parent Parent Element.
name Tag name of the Child Element.

Return

Element.

Declaration

public static Element createChildElement(Document doc, Element parent, String name) 

Method Source Code

//package com.java2s;
/*//ww w  . j a va  2 s  .  c  om
 * Copyright 2013 Keith D Swenson
 *
 * 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.
 *
 * Contributors Include: Shamim Quader, Sameer Pradhan, Kumar Raja, Jim Farris,
 * Sandia Yang, CY Chen, Rajiv Onat, Neal Wang, Dennis Tam, Shikha Srivastava,
 * Anamika Chaudhari, Ajay Kakkar, Rajeev Rastogi
 */

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

public class Main {
    /**
     * This method is used to create a Child element.
     * @param doc document on which the Child Element has to be created.
     * @param parent Parent Element.
     * @param name Tag name of the Child Element.
     * @return Element.
     */
    public static Element createChildElement(Document doc, Element parent, String name) {
        Element newElem = doc.createElement(name);
        parent.appendChild(newElem);
        return newElem;
    }

    /**
     * This method is used to create a Child Text element.
     * @param doc document on which the Text Element has to be created.
     * @param parent Parent Element.
     * @param name Tag name of the Text Element.
     * @param textValue tag value of the Text Element.
     * @return Element.
     */
    public static Element createChildElement(Document doc, Element parent, String name, String textValue) {
        //if a null is passed in, then do not create the child element
        //at all.  Then when reading, if the element does not exist,
        //the value will be null.  This is standard behaviod for
        //optional element.
        if (textValue == null) {
            return null;
        }
        Element newElem = doc.createElement(name);
        newElem.appendChild(doc.createTextNode(textValue));
        parent.appendChild(newElem);
        return newElem;
    }

    /**
     * This method is used to create an element with Attributes.
     * @param doc document on which the Child Element has to be created.
     * @param parent Parent Element.
     * @param name Tag Name of the Element
     * @param attributeNames List of Attribute names.
     * @param attributeValues List of Attribute values.
     * @return Element
     */
    public static Element createChildElement(Document doc, Element parent, String name, String textValue,
            String[] attributeNames, String[] attributeValues) {
        Element newElem = doc.createElement(name);

        if (textValue != null) {
            newElem.appendChild(doc.createTextNode(textValue));
        }

        for (int i = 0; i < attributeNames.length; i++) {
            newElem.setAttribute(attributeNames[i], attributeValues[i]);
        }

        parent.appendChild(newElem);
        return newElem;
    }
}

Related

  1. createAndAppendChild(Element parentElement, String elementName, Properties attributes)
  2. createChild(Document document, Node parent, String childNodeName)
  3. createChild(Element el, String name, boolean attach)
  4. createChild(final Element el, final String name)
  5. createChild(Node node, String elemName)
  6. createChildElement(Document doc, Node node, String nodeName)
  7. createChildElement(Element parent, String name)
  8. createChildElement(Element parentElement, String elementName)
  9. createChildElement(Element parentElement, String strTagName)