Java XML Child Element Create addElement(Document document, Element father, String name, String attOneName, String attOneValue, String attTwoName, String attTwoValue)

Here you can find the source of addElement(Document document, Element father, String name, String attOneName, String attOneValue, String attTwoName, String attTwoValue)

Description

adds an Element with two attributs to a DOM-tree.

License

Apache License

Parameter

Parameter Description
document Document: the DOM-tree to add to
father Element: the new element will be inserted directly under this Element in the tree
name String: the name of the new element
attOneName String: the name of the 1st attribut
attOneValue String: the value of the 1st attribut
attTwoName String: the name of the 2nd attribut
attTwoValue String: the value of the 2nd attribut

Declaration

public static void addElement(Document document, Element father, String name, String attOneName,
        String attOneValue, String attTwoName, String attTwoValue) 

Method Source Code

//package com.java2s;
/**/*  w  w  w  . j a v  a  2  s .c om*/
 * A utility class providing several static methods to handle DOM documents.
 * 
 * @version DESMO-J, Ver. 2.5.1d copyright (c) 2015
 * @author Nicolas Knaak and Gunnar Kiesel
 * 
 * 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.CDATASection;

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

import org.w3c.dom.Text;

public class Main {
    /**
     * adds an Element with one attribut to a DOM-tree.
     * 
     * @param document
     *            Document: the DOM-tree to add to
     * @param father
     *            Element: the new element will be inserted directly under this
     *            Element in the tree
     * @param name
     *            String: the name of the new element
     * @param attOneName
     *            String: the name of the attribut
     * @param attOneValue
     *            String: the value of the attribut
     */
    public static void addElement(Document document, Element father, String name, String attOneName,
            String attOneValue) {
        Element element = document.createElement(name);
        element.setAttribute(attOneName, attOneValue);
        father.appendChild(element);
    }

    /**
     * adds an Element with two attributs to a DOM-tree.
     * 
     * @param document
     *            Document: the DOM-tree to add to
     * @param father
     *            Element: the new element will be inserted directly under this
     *            Element in the tree
     * @param name
     *            String: the name of the new element
     * @param attOneName
     *            String: the name of the 1st attribut
     * @param attOneValue
     *            String: the value of the 1st attribut
     * @param attTwoName
     *            String: the name of the 2nd attribut
     * @param attTwoValue
     *            String: the value of the 2nd attribut
     */
    public static void addElement(Document document, Element father, String name, String attOneName,
            String attOneValue, String attTwoName, String attTwoValue) {
        Element element = document.createElement(name);
        element.setAttribute(attOneName, attOneValue);
        element.setAttribute(attTwoName, attTwoValue);
        father.appendChild(element);
    }

    /**
     * Creates a DOM element node with the given name. This node must only be
     * used in the specified document.
     * 
     * @param d
     *            the "mother" document of the created node
     * @param name
     *            the name of the node
     * @return a node with the given name
     */
    public static Element createElement(Document d, String name) {
        return d.createElement(name);
    }

    /**
     * Creates a DOM element node with the given name and contents. If indicated
     * the contents is enclosed in CDATA marks.
     * 
     * @param d
     *            the "mother" document of the created Element
     * @param name
     *            the name of the element
     * @param value
     *            the element's value
     * @param isCDATA
     *            a flag indicating if this element contains (unformatted)
     *            CDATA.
     * @return a new DOM Element
     */
    public static Element createElement(Document d, String name, String value, boolean isCDATA) {
        Element e = createElement(d, name);
        if (isCDATA)
            e.appendChild(createCDATA(d, value));
        else
            e.appendChild(createText(d, value));
        return e;
    }

    /**
     * Creates a CDATA section node in an XML document
     * 
     * @param d
     *            the "mother" document of the created text node
     * @param text
     *            the text this CDATA section contains
     * @return a new CDATA section node.
     */
    public static CDATASection createCDATA(Document d, String text) {
        return d.createCDATASection(text);
    }

    /**
     * Creates a text node in an XML document
     * 
     * @param d
     *            the "mother" document of the created text node
     * @param text
     *            contents of the text node
     * @return a new text node
     */
    public static Text createText(Document d, String text) {
        return d.createTextNode(text);
    }
}

Related

  1. addChildText(Document doc, Element parent, String textValue)
  2. addElement(Document doc, Element parent, String nodeName)
  3. addElement(Document doc, Element rootElement, String elementName, String typeIn, String isArrayIn, String partitionerIn)
  4. addElement(Document doc, Node parent, String element)
  5. addElement(Document doc, String name, Element parent)
  6. addElement(Document document, Element parentElement, String elementName, String elementValue)
  7. addElement(final Document doc, final Node parent, final String name)
  8. addElement(final Document document, final Element parentDom, final String namespace, final String name)
  9. addElement(final Document document, final Element parentDom, final String namespace, final String name)