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

/**
 * Static helper function to get the element data of the specified node.
 * //  www  .ja va 2s .c  om
 * @param node
 *            the node where the text data resides; may be <code>null</code>
 *            in which case this funtion will return ""
 * 
 * @return the complete text of the specified node, or an empty string if
 *         the node has no text or is <code>null</code>
 */
public static String getElementData(final Node node) {
    StringBuffer ret = new StringBuffer();

    if (node != null) {
        Node text;
        for (text = node.getFirstChild(); text != null; text = text.getNextSibling()) {
            /**
             * the item's value is in one or more text nodes which are its
             * immediate children
             */
            if (text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE) {
                ret.append(text.getNodeValue());
            } else {
                if (text.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
                    ret.append(getElementData(text));
                }
            }
        }
    }
    return ret.toString();
}

From source file:Main.java

protected static void getTextFromNode(Node node, StringBuffer buffer, boolean addSpace) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
    case Node.TEXT_NODE:
        buffer.append(node.getNodeValue());
        if (addSpace)
            buffer.append(" ");
    }/*from   w w  w.j av a2s.co m*/
    Node child = node.getFirstChild();
    while (child != null) {
        getTextFromNode(child, buffer, addSpace);
        child = child.getNextSibling();
    }
}

From source file:Main.java

public static final String getCDATA(Element elem, boolean trim) {
    StringBuffer sb = new StringBuffer();
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nc = nl.item(i);//from  w w w .  java2  s. co m
        if (nc.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append(((Text) nc).getData());
        } else if (nc.getNodeType() == Node.TEXT_NODE) {
            String txt = ((Text) nc).getData();
            if (trim) {
                txt = txt.trim();
            }
            sb.append(txt);
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * @param node/*  ww w .j ava2 s.  c  om*/
 * @return true if the given node is of type text or CDATA.
 */
public static boolean isText(Node node) {
    int ntype = node.getNodeType();
    return ntype == Node.TEXT_NODE || ntype == Node.CDATA_SECTION_NODE;
}

From source file:Main.java

public static final String getCDATA(Element elem, boolean trim) {
    StringBuilder sb = new StringBuilder();
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nc = nl.item(i);/*from w ww . j  a va  2 s.c  om*/
        if (nc.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append(((Text) nc).getData());
        } else if (nc.getNodeType() == Node.TEXT_NODE) {
            String txt = ((Text) nc).getData();
            if (trim) {
                txt = txt.trim();
            }
            sb.append(txt);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static StringBuffer gatherTextPCDATAAndCDATADescendants(Node node, StringBuffer sb, String separator) {
    if (sb == null)
        sb = new StringBuffer();
    int t = node.getNodeType();
    if (t == Node.CDATA_SECTION_NODE || t == Node.TEXT_NODE) {
        //sb.append("================= "+node.getNodeName()+" =================");
        sb.append(node.getNodeValue());// w w w .  j  a v  a2s. com
    } else {
        NodeList list = node.getChildNodes();
        for (int i = 0, len = list.getLength(); i < len; i++) {
            Node n = list.item(i);
            gatherTextPCDATAAndCDATADescendants(n, sb, separator);
            if (i < len - 1)
                sb.append(separator);
        }
    }
    return sb;
}

From source file:Utils.java

/**
 * <p>Returns an array of text values of a child element. Returns
 * <code>null</code> if there is no child element found.</p>
 *
 * @param parent parent element//from  w  ww  . j  a  v a  2s  .co  m
 * @param name name of the child element
 * @return text value
 */
public static String[] getChildElementTextArr(Element parent, String name) {
    // Get all the elements
    List children = getChildElementsByName(parent, name);

    String str[] = new String[children.size()];

    for (int i = 0; i < children.size(); i++) {
        Node child = (Node) children.get(i);

        StringBuffer buf = new StringBuffer();

        NodeList nodes = child.getChildNodes();
        for (int j = 0; j < nodes.getLength(); j++) {
            Node node = nodes.item(j);
            if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
                Text text = (Text) node;
                buf.append(text.getData().trim());
            }
        }

        str[i] = buf.toString();
    }

    return str;
}

From source file:Main.java

public static String getTextContent(final Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default:/*from  w w w. j  a  v a2 s  .  co  m*/
        return null;
    }
}

From source file:MainClass.java

public static void print(Node node, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        ps.print("<" + node.getNodeName());

        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ps.print(" " + map.item(i).getNodeName() + "=\"" + map.item(i).getNodeValue() + "\"");
        }/*from  w  ww .  ja  v  a  2  s  . c  o m*/
        ps.println(">");
        return;
    case Node.ATTRIBUTE_NODE:
        ps.println(node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
        return;
    case Node.TEXT_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.CDATA_SECTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        ps.println(node.getNodeName() + "=" + node.getNodeValue());
        return;
    }
}

From source file:Main.java

public static String getText(Element node) {
    StringBuffer sb = new StringBuffer();
    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);

        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            sb.append(child.getNodeValue());
        }//from   ww w.  j ava2 s  . c om
    }

    return sb.toString();
}