Example usage for org.w3c.dom Document createCDATASection

List of usage examples for org.w3c.dom Document createCDATASection

Introduction

In this page you can find the example usage for org.w3c.dom Document createCDATASection.

Prototype

public CDATASection createCDATASection(String data) throws DOMException;

Source Link

Document

Creates a CDATASection node whose value is the specified string.

Usage

From source file:Main.java

public static void createNewCDATA(Node elem, String value) {
    Document doc = elem.getOwnerDocument();
    if (value == null) {
        value = "";
    }//  w w w. j ava 2  s. c o m
    CDATASection cdata = doc.createCDATASection(value);
    elem.appendChild(cdata);
}

From source file:Main.java

public static void addCdataNode(Element element, String tagName, String value) {
    if (value != null && !(value.equals(""))) {
        Document document = element.getOwnerDocument();
        Element titleElement = document.createElement(tagName);
        titleElement.appendChild(document.createCDATASection(value));
        element.appendChild(titleElement);
    }/*from   w w  w .  j av a  2s . c  o m*/
}

From source file:Main.java

/**
 *
 * @param doc Document//from   ww  w . j  a va2  s  .  c o  m
 * @param parent Node
 * @param name String
 * @param value int
 * @return Element
 */
public static Element appendChildNodeCDATA(Document doc, Node parent, String name, int value) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createCDATASection(new Integer(value).toString()));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

/**
 *
 * @param doc Document/* w ww.  jav a2 s  .  c o  m*/
 * @param parent Node
 * @param name String
 * @param value String
 * @return Element
 */
public static Element appendChildNodeCDATA(Document doc, Node parent, String name, String value) {
    Element child = doc.createElement(name);
    child.appendChild(doc.createCDATASection(value));
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static Element createCDATAElement(String name, Object value, Document doc) {
    Element element = doc.createElement(name);
    element.appendChild(doc.createCDATASection(value.toString()));
    return element;
}

From source file:Main.java

/**
 * Creates a child element with the given name and appends it to the element child node list. Also
 * creates a CDATASection node with the given value and appends it to the new elements child node
 * list./*from   w w  w.j av a  2 s.c  o  m*/
 */
public static Element addChildElementCDATAValue(Element element, String childElementName,
        String childElementValue, Document document) {
    Element newElement = addChildElement(element, childElementName, document);

    newElement.appendChild(document.createCDATASection(childElementValue));
    return newElement;
}

From source file:Main.java

/**
 * Creates a CDATA section node in an XML document
 * /*  w  ww.  j  av  a2 s  .co  m*/
 * @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);
}

From source file:Main.java

public static Element addCDataElement(final Document dom, final Element el, final String name,
        final String value) {
    Element newElement = dom.createElement(name);
    newElement.appendChild(dom.createCDATASection(value));
    el.appendChild(newElement);//from   w w  w.  ja va 2s  . c  om
    return el;
}

From source file:Main.java

/**
 * For compatibility reasons the following is required:
 * If the value of a text node is to be changed, but a CDATA section with this name
 * already exists, the CDATA section is removed an a text node is created or changed.
 *
 * If the value of a CDATA section is to be changed, but a text node with this name
 * already exists, the text node is removed an a CDATA section is created or changed.
 *
 */// ww  w.  ja  va 2s .c o  m
public static void setElementText(Element e, String newValue, boolean cdata) {
    if (e == null) {
        return;
    }

    Node node = null;

    NodeList children = e.getChildNodes();

    if (children != null) {
        Node childToRemove = null;
        boolean changed = false;

        int listLength = children.getLength();

        for (int i = 0; i < listLength; i++) {
            node = children.item(i);

            int nodeType = node.getNodeType();

            if (nodeType == Node.TEXT_NODE) {
                if (cdata) {
                    childToRemove = node;
                } else {
                    node.setNodeValue(newValue);
                    changed = true;
                }
            }

            if (nodeType == Node.CDATA_SECTION_NODE) {
                if (!cdata) {
                    childToRemove = node;
                } else {
                    node.setNodeValue(newValue);
                    changed = true;
                }

            }
        }

        if (childToRemove != null) {
            // System.out.println("removing child " + childToRemove.getNodeValue());
            childToRemove.setNodeValue("");
            e.removeChild(childToRemove);
        }

        if (changed) {
            return;
        }
    }

    Document doc = e.getOwnerDocument();

    if (cdata) {
        node = doc.createCDATASection(newValue);
    } else {
        node = doc.createTextNode(newValue);
    }

    e.appendChild(node);
}

From source file:Main.java

public static void addCDATA(Document doc) {
    Element root = doc.getDocumentElement();
    Element place = (Element) root.getFirstChild();
    Element directions = (Element) place.getLastChild();
    String dirtext = "cdData.";
    CDATASection dirdata = doc.createCDATASection(dirtext);
    directions.replaceChild(dirdata, directions.getFirstChild());
}