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

Here you can find the source of createElementLn(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 createElementLn(Document d, String name, String value, boolean isCDATA) 

Method Source Code

//package com.java2s;
/**/*w  ww  . j  av 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 and contents. If indicated
     * the contents is enclosed in CDATA marks. The tag ends with a new line
     * character.
     * 
     * @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 createElementLn(Document d, String name, String value, boolean isCDATA) {
        Element e = createElement(d, name);
        if (isCDATA) {
            e.appendChild(createTextLn(d));
            e.appendChild(createCDATA(d, value));
            e.appendChild(createTextLn(d));
        } else {
            e.appendChild(createTextLn(d, "\n" + value));
        }
        return e;
    }

    /**
     * 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 text node in an XML document ended by a new line character
     * 
     * @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 createTextLn(Document d, String text) {
        return createText(d, text + "\n");
    }

    /**
     * Creates a text node in an XML document representing a new line character
     * 
     * @param d
     *            the "mother" document of the created text node
     * @return a text node containing a single newline character
     */
    public static Text createTextLn(Document d) {
        return createTextLn(d, "");
    }

    /**
     * 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. createElement(String tag, String textContent, Document xml)
  2. createElement(String tagName, String text, Document doc)
  3. createElementAndText(Element parent, String elementName, String text)
  4. createElementInSameNamespace(Element parent, String localName)
  5. createElementInTempDocument(String name, String prefix, String namespaceURI)
  6. createElementMapping(final Document doc1, final Document doc2)
  7. createElementMappingForNodes(final Node n1, final Node n2, final Map mapping)
  8. createElementNS(Document doc, String namespaceURI, String prefix, String nodeName)
  9. createElementNS(Document root, String namespaceURI, String qualifiedName)