Java XML Element Create createElement(Document d, String name, String value, boolean isCDATA)

Here you can find the source of createElement(Document d, String name, String value, boolean isCDATA)

Description

Creates a DOM element node with the given name and contents.

License

Apache License

Parameter

Parameter Description
d the "mother" document of the created Element
name the name of the element
value the element's value
isCDATA a flag indicating if this element contains (unformatted) CDATA.

Return

a new DOM Element

Declaration

public static Element createElement(Document d, String name, String value, boolean isCDATA) 

Method Source Code

//package com.java2s;
/**//from   ww w.  jav a  2 s .  c o m
 * 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 {
    /**
     * 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. addNewElementWithAttribute(Document xmlDoc, Node node, String elementName, String elementValue, String attrName, String attrValue)
  2. createChildElement(Document doc, Element parent, String name, String textValue, String[] attributeNames, String[] attributeValues)
  3. createChildInternal(Document document, Node parent, String nodeName, String... attr_name_and_value)
  4. createComment(Element parent, String str)
  5. createElement(Document d, String name, String value)
  6. createElement(Document d, String tagName)
  7. createElement(Document doc, Node parent, int depth, String name, String contents)
  8. createElement(Document doc, Node parent, String tagName)
  9. createElement(Document doc, String elementNamespace, String elementName)