Example usage for org.w3c.dom Node CDATA_SECTION_NODE

List of usage examples for org.w3c.dom Node CDATA_SECTION_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node CDATA_SECTION_NODE.

Prototype

short CDATA_SECTION_NODE

To view the source code for org.w3c.dom Node CDATA_SECTION_NODE.

Click Source Link

Document

The node is a CDATASection.

Usage

From source file:Main.java

public static void stripWhitespace(Element element) {
    final NodeList childNodeList = element.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node node = childNodeList.item(i);
        switch (node.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            final String text = ((CharacterData) node).getData();
            if (WhitespacePattern.matcher(text).matches()) {
                element.removeChild(node);
                --i;//  w w  w. j av  a2 s. c o m
            }
            break;
        case Node.ELEMENT_NODE:
            stripWhitespace((Element) node);
            break;
        }
    }
}

From source file:Main.java

public static String getElementText(Element e) {
    if (e == null) {
        return ("");
    }//from   w ww  .ja  v a2s . c  o  m

    NodeList children = e.getChildNodes();

    if (children == null) {
        return ("");
    }

    StringBuffer text = new StringBuffer();

    int listLength = children.getLength();

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

        int nodeType = node.getNodeType();

        if ((nodeType == Node.TEXT_NODE) || (nodeType == Node.CDATA_SECTION_NODE)) {
            String nodeValue = node.getNodeValue();
            if (nodeValue != null) {
                text.append(nodeValue);
            }
        }
    }

    return (text.toString().trim());
}

From source file:Main.java

/**
 * Get the element text node.  Locate the text node and return it.  For example;
 * <element>this is the text node</element>
 *
 * @param element The element to get the text node for
 * @return The text node/*from w  w w.  jav a2 s . com*/
 */
public static Node getElementTextNode(Element element) {

    Node textNode = null;

    // go through each child element
    Node node;
    short nodeType;
    NodeList children = element.getChildNodes();
    for (int ii = 0; ii < children.getLength(); ii++) {
        node = children.item(ii);
        nodeType = node.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            textNode = node;
            break;
        } else if (nodeType == Node.CDATA_SECTION_NODE) {
            textNode = node;
            break;
        }
    }
    return (textNode);
}

From source file:Main.java

/** Transform xml element and children into a string
 *
 * @param nd Node root of elements to transform
 * @return String representation of xml/*from   w w w.j  av  a 2  s  .c  o  m*/
 */
public static String elementToString(Node nd, boolean add_newlines) {
    //short type = n.getNodeType();

    if (Node.CDATA_SECTION_NODE == nd.getNodeType()) {
        return "<![CDATA[" + nd.getNodeValue() + "]]&gt;";
    }

    // return if simple element type
    final String name = nd.getNodeName();
    if (name.startsWith("#")) {
        if (name.equals("#text"))
            return nd.getNodeValue();
        return "";
    }

    // output name
    String ret = "<" + name;

    // output attributes
    NamedNodeMap attrs = nd.getAttributes();
    if (attrs != null) {
        for (int idx = 0; idx < attrs.getLength(); idx++) {
            Node attr = attrs.item(idx);
            ret += " " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"";
        }
    }

    final String text = nd.getTextContent();
    final NodeList child_ndls = nd.getChildNodes();
    String all_child_str = "";

    for (int idx = 0; idx < child_ndls.getLength(); idx++) {
        final String child_str = elementToString(child_ndls.item(idx), add_newlines);
        if ((child_str != null) && (child_str.length() > 0)) {
            all_child_str += child_str;
        }
    }
    if (all_child_str.length() > 0) {
        // output children
        ret += ">" + (add_newlines ? "\n" : " ");
        ret += all_child_str;
        ret += "</" + name + ">";
    } else if ((text != null) && (text.length() > 0)) {
        // output text
        ret += text;
        ret += "</" + name + ">";
    } else {
        // output nothing
        ret += "/>" + (add_newlines ? "\n" : " ");
    }

    return ret;
}

From source file:Main.java

public static final Node copyElement(Node e) throws UnsupportedDataTypeException {
    Node result = null;/*  ww w  . j a  va 2 s  . co  m*/

    switch (e.getNodeType()) {
    case Node.ELEMENT_NODE:
        result = document.createElement(e.getNodeName());

        for (int i = 0; i < e.getAttributes().getLength(); i++) {
            Attr attr = document.createAttribute(e.getAttributes().item(i).getNodeName());
            attr.setNodeValue(e.getAttributes().item(i).getNodeValue());
            result.getAttributes().setNamedItem(attr);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        result = document.createCDATASection(((CDATASection) e).getData());
        break;
    case Node.TEXT_NODE:
        if (((Text) e).getTextContent().replaceAll("\t", "").trim() != "")
            result = document.createTextNode(((Text) e).getTextContent().replaceAll("\t", "").trim());
        break;

    default:
        throw new UnsupportedDataTypeException(new StringBuilder(e.getNodeType()).toString());
    }

    for (int i = 0; i < e.getChildNodes().getLength(); i++)
        result.appendChild(copyElement(e.getChildNodes().item(i)));

    return result;
}

From source file:Main.java

/**
 * Is the supplied W3C DOM Node a text node.
 *
 * @param node The node to be tested.//from ww w.  j  a  v a  2 s  .  co  m
 * @return True if the node is a text node, otherwise false.
 */
public static boolean isTextNode(Node node) {
    short nodeType;

    if (node == null) {
        return false;
    }
    nodeType = node.getNodeType();

    return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
}

From source file:Main.java

/**
 * Return the text (node value) of the first node under this, works best if normalized.
 *///from  w  ww. j a va2  s .c om
public static String elementValue(Element element) {
    if (element == null)
        return null;
    // make sure we get all the text there...
    element.normalize();
    Node textNode = element.getFirstChild();

    if (textNode == null)
        return null;

    StringBuffer valueBuffer = new StringBuffer();
    do {
        if (textNode.getNodeType() == Node.CDATA_SECTION_NODE || textNode.getNodeType() == Node.TEXT_NODE) {
            valueBuffer.append(textNode.getNodeValue());
        }
    } while ((textNode = textNode.getNextSibling()) != null);
    return valueBuffer.toString();
}

From source file:Main.java

/**
 * return the text content of an element
 *//*from  w  w w.  j  av a2  s .  c  o  m*/
public static String getTextContent(org.w3c.dom.Node element) {
    StringBuffer childtext = new StringBuffer();
    NodeList childlist = element.getChildNodes();
    int ct = childlist.getLength();

    for (int j = 0; j < ct; j++) {
        org.w3c.dom.Node childNode = childlist.item(j);

        if ((childNode.getNodeType() == Node.TEXT_NODE)
                || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
            childtext.append(childNode.getNodeValue().trim());
        }
    }

    return childtext.toString();
}

From source file:Main.java

public static String getText(final Node node) {
    final StringBuilder result = new StringBuilder();
    if (!node.hasChildNodes())
        return "";

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node subnode = list.item(i);
        if (subnode.getNodeType() == Node.TEXT_NODE) {
            result.append(subnode.getNodeValue());
        } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) {
            result.append(subnode.getNodeValue());
        } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
            // Recurse into the subtree for text
            // (and ignore comments)
            result.append(getText(subnode));
        }//from  w  w  w  .j  a  va 2 s .co m
    }

    return result.toString();
}

From source file:Main.java

private static Node getChildNodeByType(Element element, short nodeType) {
    if (element == null) {
        return null;
    }// w  w w  .  j a  v  a  2  s  .c o m

    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() < 1) {
        return null;
    }

    Node node;
    String data;
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        short type = node.getNodeType();
        if (type == nodeType) {
            if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
                data = ((Text) node).getData();
                if (data == null || data.trim().length() < 1) {
                    continue;
                }
            }

            return node;
        }
    }

    return null;
}