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

/**
 * Returns the text associated to the given node
 * i.e. the value of the//from w  w  w .  j  a va2  s.  c  o m
 * @param node
 * @return
 */
public static String getText(Node node) {
    Node textNode = node.getFirstChild();
    if (textNode == null) {
        return null;
    }
    short nodeType = textNode.getNodeType();
    if (nodeType != Node.TEXT_NODE && nodeType != Node.CDATA_SECTION_NODE) {
        return null;
    }
    return textNode.getNodeValue().trim();
}

From source file:Utils.java

/**
 * /*from   w w w.  j a v  a 2 s  . c  om*/
 */
public static String getElementText(Element element) {
    StringBuffer buffer = new StringBuffer();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
            buffer.append(node.getNodeValue());
        }
    }
    return buffer.toString();
}

From source file:Main.java

public static String elementToString(Node n) {

    String name = n.getNodeName();

    short type = n.getNodeType();

    if (Node.CDATA_SECTION_NODE == type) {
        return "<![CDATA[" + n.getNodeValue() + "]]&gt;";
    }//from  w  w w.  j av a  2  s  . co m

    if (name.startsWith("#")) {
        return "";
    }

    StringBuilder sb = new StringBuilder();
    sb.append('<').append(name);

    NamedNodeMap attrs = n.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\"");
        }
    }

    String textContent = null;
    NodeList children = n.getChildNodes();

    if (children.getLength() == 0) {
        //      if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) {
        if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) {
            sb.append(textContent).append("</").append(name).append('>');

        } else {
            sb.append("/>");
        }
    } else {
        sb.append('>');
        boolean hasValidChildren = false;
        for (int i = 0; i < children.getLength(); i++) {
            String childToString = elementToString(children.item(i));
            if (!"".equals(childToString)) {
                sb.append('\n').append(childToString);
                hasValidChildren = true;
            }
        }
        if (hasValidChildren) {
            sb.append('\n');
        }

        //      if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) {
        if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) {
            sb.append(textContent);
        }

        sb.append("</").append(name).append('>');
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Gets <code>CDATASection</code> element for node.
 *
 * @param node node with text.//from w  ww  . ja va 2  s .c om
 * @return text, that contains the specified node.
 */
public static String getNodeCDATASection(final Node node) {
    //        node.normalize();
    final NodeList list = node.getChildNodes();
    for (int i = 0, len = list.getLength(); i < len; i++) {
        final Node child = list.item(i);
        if (child.getNodeType() == Node.CDATA_SECTION_NODE)
            return child.getNodeValue();
    }
    return null;
}

From source file:Main.java

/**
 * Returns element's CDATA child node (if it has one).
 * @param element the element whose CDATA we need to get.
 * @return a CDATASection object containing the specified element's CDATA
 * content//from  w w  w. jav a  2s  .  c o  m
 */
public static CDATASection getCDataNode(Element element) {
    return (CDATASection) getChildByType(element, Node.CDATA_SECTION_NODE);
}

From source file:Main.java

/**
 * Collapses a list of CDATASection, Text, and predefined EntityReference
 * nodes into a single string.  If the list contains other types of nodes,
 * those other nodes are ignored./*from w  ww . j  a v a  2  s.c o m*/
 */
public static String getText(NodeList nodeList) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        switch (node.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            buffer.append(node.getNodeValue());
            break;
        case Node.ENTITY_REFERENCE_NODE:
            if (node.getNodeName().equals("amp"))
                buffer.append('&');
            else if (node.getNodeName().equals("lt"))
                buffer.append('<');
            else if (node.getNodeName().equals("gt"))
                buffer.append('>');
            else if (node.getNodeName().equals("apos"))
                buffer.append('\'');
            else if (node.getNodeName().equals("quot"))
                buffer.append('"');
            // Any other entity references are ignored
            break;
        default:
            // All other nodes are ignored
        }
    }
    return buffer.toString();
}

From source file:Main.java

/**
 *   Gets the text for a node by concatenating child TEXT elements.
 *///from w  ww  .jav  a2 s .co  m
public synchronized static String getText(Node n) {
    if (n == null)
        throw new IllegalArgumentException("Node argument cannot be null");

    StringBuilder b = new StringBuilder();
    NodeList nl = n.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE)
            b.append(nl.item(i).getNodeValue());
        else if (nl.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
            b.append(nl.item(i).getNodeValue());
    }

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

From source file:Utils.java

public static String elementToString(Node n) {

    String name = n.getNodeName();

    short type = n.getNodeType();

    if (Node.CDATA_SECTION_NODE == type) {
        return "<![CDATA[" + n.getNodeValue() + "]]&gt;";
    }//from   www.  j a  v a 2 s.  co m

    if (name.startsWith("#")) {
        return "";
    }

    StringBuffer sb = new StringBuffer();
    sb.append('<').append(name);

    NamedNodeMap attrs = n.getAttributes();
    if (attrs != null) {
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()).append("\"");
        }
    }

    String textContent = null;
    NodeList children = n.getChildNodes();

    if (children.getLength() == 0) {
        if ((textContent = XMLUtil.getTextContent(n)) != null && !"".equals(textContent)) {
            sb.append(textContent).append("</").append(name).append('>');
            ;
        } else {
            sb.append("/>").append('\n');
        }
    } else {
        sb.append('>').append('\n');
        boolean hasValidChildren = false;
        for (int i = 0; i < children.getLength(); i++) {
            String childToString = elementToString(children.item(i));
            if (!"".equals(childToString)) {
                sb.append(childToString);
                hasValidChildren = true;
            }
        }

        if (!hasValidChildren && ((textContent = XMLUtil.getTextContent(n)) != null)) {
            sb.append(textContent);
        }

        sb.append("</").append(name).append('>');
    }

    return sb.toString();
}

From source file:Main.java

/**
 * @param node/*from   w w w.ja  v  a  2 s  .c  om*/
 */
public static void displayNodeInfo(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node ");
        break;
    case Node.ELEMENT_NODE:
        System.out.println("Element Node: " + node.getNodeName());
        break;
    case Node.TEXT_NODE:
        System.out.println("Text Node: " + node.getNodeName());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.println("CDATA Section Node: ");
        break;
    case Node.COMMENT_NODE:
        System.out.println("Comment Node ");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Processing Instruction Node ");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Reference Node ");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Type Node ");
        break;
    }
}

From source file:Main.java

/**
 * Gets the text value of the given element.
 * //from  w  w w  . java 2 s  .  c  om
 * @param e the element.
 * @return the text contents of the given element.s
 */
public static String getText(Element e) {
    StringBuilder sb = null;

    if (e != null) {
        NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);

            switch (node.getNodeType()) {
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                if (sb == null) {
                    sb = new StringBuilder();
                }
                sb.append(node.getNodeValue());
                break;
            }
        }
    }

    return (sb != null) ? sb.toString() : null;
}