Java XML CDATA Append appendCDATAElement(Element parent, String tagName, String value)

Here you can find the source of appendCDATAElement(Element parent, String tagName, String value)

Description

Appends the CDATA element to the parent element.

License

Apache License

Parameter

Parameter Description
parent the parent element
tagName the CDATA element name
value the CDATA element value

Return

the CDATA element added to the parent element

Declaration

public static Element appendCDATAElement(Element parent, String tagName, String value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**/*from   w w  w  .  j  ava  2s  .  c o m*/
     * Appends the CDATA element to the parent element.
     *
     * @param parent
     *          the parent element
     * @param tagName
     *          the CDATA element name
     * @param value
     *          the CDATA element value
     * @return the CDATA element added to the parent element
     */
    public static Element appendCDATAElement(Element parent, String tagName, String value) {
        Element child = appendElement(parent, tagName);
        if (value == null) { // avoid "null" word in the XML payload
            value = "";
        }

        Node cdata = child.getOwnerDocument().createCDATASection(value);
        child.appendChild(cdata);
        return child;
    }

    /**
     * Appends the child element to the parent element.
     *
     * @param parent
     *          the parent element
     * @param tagName
     *          the child element name
     * @return the child element added to the parent element
     */
    public static Element appendElement(Element parent, String tagName) {
        Element child = parent.getOwnerDocument().createElement(tagName);
        parent.appendChild(child);
        return child;
    }

    /**
     * Appends the child element as well as value to the parent element.
     *
     * @param parent
     *          the parent element
     * @param tagName
     *          the child element name
     * @param value
     *          the child element value
     * @return the child element added to the parent element
     */
    public static Element appendElement(Element parent, String tagName, String value) {
        Element child = appendElement(parent, tagName);
        child.setTextContent(value);
        return child;
    }

    /**
     * Appends another element as a child element.
     *
     * @param parent
     *          the parent element
     * @param child
     *          the child element to append
     */
    public static void appendElement(Element parent, Element child) {
        Node tmp = parent.getOwnerDocument().importNode(child, true);
        parent.appendChild(tmp);
    }
}

Related

  1. appendCDATA(Element parent, String content)
  2. appendCDATAElement(Element parent, String name, String content)
  3. appendCDATAElement(Element parent, String tagName, String value)
  4. appendCDATASection(CDATASection cdataSection, StringBuffer buf)
  5. appendCDATASection(Node parent, String name, Object data)
  6. appendCDATASubNode(String elementName, String nodeData, Element parentElement, Document doc)
  7. setCDATA(Element element, String data)