Java Utililty Methods XML CData

List of utility methods to do XML CData

Description

The list of methods to do XML CData are organized into topic(s).

Method

ElementcreateNewCDATAElement(Node elem, String tag, String value)
create New CDATA Element
Document doc = elem.getOwnerDocument();
Element res = doc.createElement(tag);
CDATASection cdata = doc.createCDATASection(value);
res.appendChild(cdata);
elem.appendChild(res);
return res;
StringgetCharacterData(Element el)
Gets the character data corresponding to the given DOM element.
Text text = getChildTextNode(el);
return text == null ? null : text.getData();
StringgetCharacterDataFromElement(Element e)
get Character Data From Element
org.w3c.dom.Node child = e.getFirstChild();
if (child instanceof CharacterData) {
    CharacterData cd = (CharacterData) child;
    return cd.getData();
return "?";
StringgetCharacterDataFromElement(Element e)
get Character Data From Element
if (e != null) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
        CharacterData cd = (CharacterData) child;
        return cd.getData();
return "?";
...
StringgetCharacterDataFromElement(Element e)
get Character Data From Element
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
    CharacterData cd = (CharacterData) child;
    return cd.getData();
} else {
    return "?";
StringgetCharacterDataFromElementWithKey(final Element element, final String key)
get Character Data From Element With Key
String result = null;
if (element != null) {
    final NodeList jobNodeList = element.getElementsByTagName(key);
    final Node node = jobNodeList.item(0);
    if (node != null) {
        final CharacterData characterData = (CharacterData) jobNodeList.item(0).getChildNodes().item(0);
        if (characterData != null) {
            result = characterData.getNodeValue();
...
booleanisCDataOrText(int nodeType)
is C Data Or Text
return Node.CDATA_SECTION_NODE == nodeType || Node.TEXT_NODE == nodeType;
StringparseCdataSection(Node cdataParentNode)
parse Cdata Section
if (cdataParentNode == null)
    return "";
StringBuilder sb = new StringBuilder();
NodeList cdataParentNodeChildren = cdataParentNode.getChildNodes();
if (cdataParentNodeChildren != null && cdataParentNodeChildren.getLength() > 0) {
    for (int i = 0; i < cdataParentNodeChildren.getLength(); i++) {
        Node cdataNode = cdataParentNodeChildren.item(i);
        if (CDATA_SECTION.equalsIgnoreCase(cdataNode.getNodeName()))
...
voidremoveEmptyCDATASections(Node node)
Remove empty CDATA sections from the given node and all of its descendants.
Node child = node.getFirstChild();
while (child != null) {
    Node next = child.getNextSibling();
    switch (child.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        if (child.getNodeValue().length() == 0) {
            child.getParentNode().removeChild(child);
        break;
    case Node.ELEMENT_NODE:
        removeEmptyCDATASections(child);
    child = next;
voidsetTextValue(Node node, String value, boolean cdata)
set Text Value
if (node != null) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child.getNodeType() == Node.TEXT_NODE && !cdata) {
                ((Text) child).setNodeValue(value);
                return;
            if (child.getNodeType() == Node.CDATA_SECTION_NODE && cdata) {
...