Java XML Element Create createChildElement(Document doc, Element parent, String name, String textValue, String[] attributeNames, String[] attributeValues)

Here you can find the source of createChildElement(Document doc, Element parent, String name, String textValue, String[] attributeNames, String[] attributeValues)

Description

This method is used to create an element with Attributes.

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 Element
attributeNames List of Attribute names.
attributeValues List of Attribute values.

Return

Element

Declaration

public static Element createChildElement(Document doc, Element parent, String name, String textValue,
        String[] attributeNames, String[] attributeValues) 

Method Source Code

//package com.java2s;
/*/*  w  w w  . jav a2  s .c  o m*/
 * 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. addNewElementWithAttribute(Document xmlDoc, Node node, String elementName, String elementValue, String attrName, String attrValue)
  2. createChildInternal(Document document, Node parent, String nodeName, String... attr_name_and_value)
  3. createComment(Element parent, String str)
  4. createElement(Document d, String name, String value)
  5. createElement(Document d, String name, String value, boolean isCDATA)