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:DOMTreeFull.java

public static String toString(Node node) {
    StringBuffer sb = new StringBuffer();

    // is there anything to do?
    if (node == null) {
        return "";
    }//  w  w  w.  ja v  a2s  . com

    int type = node.getNodeType();
    sb.append(whatArray[type]);
    sb.append(" : ");
    sb.append(node.getNodeName());
    String value = node.getNodeValue();
    if (value != null) {
        sb.append(" Value: \"");
        sb.append(value);
        sb.append("\"");
    }

    switch (type) {

    // document
    case Node.DOCUMENT_NODE: {
        break;
    }

    // element with attributes
    case Node.ELEMENT_NODE: {
        Attr attrs[] = sortAttributes(node.getAttributes());
        if (attrs.length > 0)
            sb.append(" ATTRS:");
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];

            sb.append(' ');
            sb.append(attr.getNodeName());
            sb.append("=\"");
            sb.append(normalize(attr.getNodeValue()));
            sb.append('"');
        }
        sb.append('>');
        break;
    }

    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        break;
    }

    // cdata sections
    case Node.CDATA_SECTION_NODE: {
        break;
    }

    // text
    case Node.TEXT_NODE: {
        break;
    }

    // processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        break;
    }

    // comment node
    case Node.COMMENT_NODE: {
        break;
    }
    // DOCTYPE node
    case Node.DOCUMENT_TYPE_NODE: {
        break;
    }
    // Notation node
    case Node.NOTATION_NODE: {
        sb.append("public:");
        String id = ((Notation) node).getPublicId();
        if (id == null) {
            sb.append("PUBLIC ");
            sb.append(id);
            sb.append(" ");
        }
        id = ((Notation) node).getSystemId();
        if (id == null) {
            sb.append("system: ");
            sb.append(id);
            sb.append(" ");
        }
        break;
    }
    }
    return sb.toString();
}

From source file:kenh.xscript.ScriptUtils.java

/**
 * Check if <code>Node</code> has text.
 * @param node/*from  w  ww  .  ja va  2  s. c  o m*/
 * @return
 */
private static boolean includeTextNode(Node node) {
    short type = node.getNodeType();
    switch (type) {

    case Node.ELEMENT_NODE:
        NodeList nodes = node.getChildNodes();
        if (nodes == null)
            return false;

        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.TEXT_NODE) {
                String text = child.getNodeValue();
                if (StringUtils.isNotBlank(text))
                    return true;
            } else if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
                return true;
            }
        }
        return false;
    case Node.TEXT_NODE:
        return true;
    default:
        return false;
    }
}

From source file:DomPrintUtil.java

/**
 * Returns XML text converted from the target DOM
 * /* ww w  . ja va 2 s.  c  om*/
 * @return String format XML converted from the target DOM
 */
public String toXMLString() {
    StringBuffer tmpSB = new StringBuffer(8192);

    TreeWalkerImpl treeWalker = new TreeWalkerImpl(document, whatToShow, nodeFilter, entityReferenceExpansion);

    String lt = escapeTagBracket ? ESC_LT : LT;
    String gt = escapeTagBracket ? ESC_GT : GT;
    String line_sep = indent ? LINE_SEP : EMPTY_STR;

    Node tmpN = treeWalker.nextNode();
    boolean prevIsText = false;

    String indentS = EMPTY_STR;
    while (tmpN != null) {
        short type = tmpN.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE:
            if (prevIsText) {
                tmpSB.append(line_sep);
            }
            tmpSB.append(indentS + lt + tmpN.getNodeName());
            NamedNodeMap attrs = tmpN.getAttributes();
            int len = attrs.getLength();
            for (int i = 0; i < len; i++) {
                Node attr = attrs.item(i);
                String value = attr.getNodeValue();
                if (null != value) {
                    tmpSB.append(getAttributeString((Element) tmpN, attr));
                }
            }
            if (tmpN instanceof HTMLTitleElement && !tmpN.hasChildNodes()) {
                tmpSB.append(gt + ((HTMLTitleElement) tmpN).getText());
                prevIsText = true;
            } else if (checkNewLine(tmpN)) {
                tmpSB.append(gt + line_sep);
                prevIsText = false;
            } else {
                tmpSB.append(gt);
                prevIsText = true;
            }
            break;
        case Node.TEXT_NODE:
            if (!prevIsText) {
                tmpSB.append(indentS);
            }
            tmpSB.append(getXMLString(tmpN.getNodeValue()));
            prevIsText = true;
            break;
        case Node.COMMENT_NODE:
            String comment;
            if (escapeTagBracket) {
                comment = getXMLString(tmpN.getNodeValue());
            } else {
                comment = tmpN.getNodeValue();
            }
            tmpSB.append(line_sep + indentS + lt + "!--" + comment + "--" + gt + line_sep);
            prevIsText = false;
            break;
        case Node.CDATA_SECTION_NODE:
            tmpSB.append(line_sep + indentS + lt + "!CDATA[" + tmpN.getNodeValue() + "]]" + line_sep);
            break;
        case Node.DOCUMENT_TYPE_NODE:
            if (tmpN instanceof DocumentType) {
                DocumentType docType = (DocumentType) tmpN;
                String pubId = docType.getPublicId();
                String sysId = docType.getSystemId();
                if (null != pubId && pubId.length() > 0) {
                    if (null != sysId && sysId.length() > 0) {
                        tmpSB.append(lt + "!DOCTYPE " + docType.getName() + " PUBLIC \"" + pubId + " \"" + sysId
                                + "\">" + line_sep);
                    } else {
                        tmpSB.append(
                                lt + "!DOCTYPE " + docType.getName() + " PUBLIC \"" + pubId + "\">" + line_sep);
                    }
                } else {
                    tmpSB.append(lt + "!DOCTYPE " + docType.getName() + " SYSTEM \"" + docType.getSystemId()
                            + "\">" + line_sep);

                }
            } else {
                System.out.println("Document Type node does not implement DocumentType: " + tmpN);
            }
            break;
        default:
            System.out.println(tmpN.getNodeType() + " : " + tmpN.getNodeName());
        }

        Node next = treeWalker.firstChild();
        if (null != next) {
            if (indent && type == Node.ELEMENT_NODE) {
                indentS = indentS + " ";
            }
            tmpN = next;
            continue;
        }

        if (tmpN.getNodeType() == Node.ELEMENT_NODE) {
            tmpSB.append(lt + "/" + tmpN.getNodeName() + gt + line_sep);
            prevIsText = false;
        }

        next = treeWalker.nextSibling();
        if (null != next) {
            tmpN = next;
            continue;
        }

        tmpN = null;
        next = treeWalker.parentNode();
        while (null != next) {
            if (next.getNodeType() == Node.ELEMENT_NODE) {
                if (indent) {
                    if (indentS.length() > 0) {
                        indentS = indentS.substring(1);
                    } else {
                        System.err.println("indent: " + next.getNodeName() + " " + next);
                    }
                }
                tmpSB.append(line_sep + indentS + lt + "/" + next.getNodeName() + gt + line_sep);
                prevIsText = false;
            }
            next = treeWalker.nextSibling();
            if (null != next) {
                tmpN = next;
                break;
            }
            next = treeWalker.parentNode();
        }
    }
    return tmpSB.toString();
}

From source file:com.nridge.core.base.std.XMLUtl.java

public static String getNodeCDATAValue(Node aNode) {
    int count;/*from ww  w.ja  va2s. c om*/
    Node textNode;
    NodeList nodeList;

    nodeList = aNode.getChildNodes();
    count = nodeList.getLength();

    for (int i = 0; i < count; i++) {
        textNode = nodeList.item(i);

        if (textNode.getNodeType() == Node.CDATA_SECTION_NODE)
            return textNode.getNodeValue();
    }

    return StringUtils.EMPTY;
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * //from  w w  w. j  a  v  a2  s. c  o  m
 * @param    out            The <CODE>PrintWriter</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(PrintWriter, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintWriter out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:net.vexelon.bgrates.Utils.java

/**
 * Serialize an XML element recursively//  w w w . ja v  a 2s.c o m
 * @param node
 * @param serializer
 * @throws IOException
 */
private static void serializeXmlElement(Node node, XmlSerializer serializer) throws IOException {

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node current = children.item(i);

        if (current.getNodeType() == Node.ELEMENT_NODE) {
            Element child = (Element) current;
            serializer.startTag("", child.getNodeName());
            serializeXmlElement(child, serializer);
            serializer.endTag("", child.getNodeName());
        } else if (current.getNodeType() == Node.TEXT_NODE) {
            Text child = (Text) current;
            serializer.text(child.getData());
        } else if (current.getNodeType() == Node.CDATA_SECTION_NODE) {
            CDATASection child = (CDATASection) current;
            serializer.cdsect(child.getData());
        } else if (current.getNodeType() == Node.COMMENT_NODE) {
            Comment child = (Comment) current;
            serializer.comment(child.getData());
        }
    }
}

From source file:Main.java

/**
 * Generates XPath expression with the option of the Node values appended
 *
 * @param node             the Node whose XPath is to be found
 * @param parentXPath      the XPath of the parent Node
 * @param ignoreWhitespace the flag to indicate if Whitespace will be ignored
 * @param includeValues    the flag to indicate if Node values will be included
 * @param noIndex          the flag to indicate if Node indexes are included
 * @return the XPath string representation of the Node
 *//*from  w ww  .ja  v a 2 s .co  m*/

public static String generateXPath(Node node, String parentXPath, boolean ignoreWhitespace,
        boolean includeValues, boolean noIndex)

{

    boolean noValues = !includeValues;

    if (node == null)

        return "";

    Node parent = node.getParentNode();

    int index = noIndex ? 0 : getXPathNodeIndex(node, ignoreWhitespace);

    String indexStr = "";

    if (index > 0)

        indexStr = "[" + Integer.toString(index) + "]";

    if (node.getNodeType() == Node.DOCUMENT_NODE)

    {

        // return only the blank String, since all the other types are preceded with /

        return parentXPath + "";

    } else if (node.getNodeType() == Node.TEXT_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/TEXT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ELEMENT_NODE)

    {

        return parentXPath +

                "/" + node.getNodeName() + indexStr;

    } else if (node.getNodeType() == Node.COMMENT_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/COMMENT(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/EntityReference(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() + indexStr
                        : "/PI(" + node.getNodeValue() + ")" + indexStr);

    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE)

    {

        return parentXPath + "/[@" + node.getNodeName() +

                (noValues ? "" : "=" + node.getNodeValue()) + "]";

    } else if (node.getNodeType() == Node.DOCUMENT_TYPE_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() : "/DOCTYPE(" + node.getNodeName() + ")");

    } else if (node.getNodeType() == Node.CDATA_SECTION_NODE)

    {

        return parentXPath +

                (noValues ? "/" + node.getNodeValue() : "/CDATA(" + node.getNodeName() + ")");

    }

    // Wont reach this far but just in case

    return "";

}

From source file:com.gatf.executor.validator.ResponseValidator.java

public static String getXMLNodeValue(Node node) {
    String xmlValue = null;//w  w w.jav a2  s . c  o m
    if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
        xmlValue = node.getNodeValue();
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        xmlValue = ((Attr) node).getValue();
    } else if (node.getNodeType() == Node.ELEMENT_NODE && node.getChildNodes().getLength() >= 1
            && (node.getFirstChild().getNodeType() == Node.TEXT_NODE
                    || node.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)) {
        xmlValue = node.getFirstChild().getNodeValue();
    }
    return xmlValue;
}

From source file:Counter.java

/** Traverses the specified node, recursively. */
public void count(Node node) {

    // is there anything to do?
    if (node == null) {
        return;//www.  j av  a 2s  . c o  m
    }

    int type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        fElements = 0;
        fAttributes = 0;
        fCharacters = 0;
        fIgnorableWhitespace = 0;
        Document document = (Document) node;
        count(document.getDocumentElement());
        break;
    }

    case Node.ELEMENT_NODE: {
        fElements++;
        NamedNodeMap attrs = node.getAttributes();
        if (attrs != null) {
            fAttributes += attrs.getLength();
        }
        // drop through to entity reference
    }

    case Node.ENTITY_REFERENCE_NODE: {
        Node child = node.getFirstChild();
        while (child != null) {
            count(child);
            child = child.getNextSibling();
        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        fCharacters += ((Text) node).getLength();
        break;
    }

    case Node.TEXT_NODE: {
        if (fDocumentInfo != null) {
            Text text = (Text) node;
            int length = text.getLength();
            if (fDocumentInfo.isIgnorableWhitespace(text)) {
                fIgnorableWhitespace += length;
            } else {
                fCharacters += length;
            }
        }
        break;
    }
    }

}

From source file:cc.siara.csv_ml_demo.MainActivity.java

/**
 * Evaluates given XPath from Input box against Document generated by
 * parsing csv_ml in input box and sets value or node list to output box.
 *///from   w  w  w .  j  a  v  a2 s.c o  m
void processXPath() {
    EditText etInput = (EditText) findViewById(R.id.etInput);
    EditText etXPath = (EditText) findViewById(R.id.etXPath);
    CheckBox cbPretty = (CheckBox) findViewById(R.id.cbPretty);
    XPath xpath = XPathFactory.newInstance().newXPath();
    MultiLevelCSVParser parser = new MultiLevelCSVParser();
    Document doc = null;
    try {
        doc = parser.parseToDOM(new StringReader(etInput.getText().toString()), false);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    if (doc == null)
        return;
    StringBuffer out_str = new StringBuffer();
    try {
        XPathExpression expr = xpath.compile(etXPath.getText().toString());
        try {
            Document outDoc = Util.parseXMLToDOM("<output></output>");
            Element rootElement = outDoc.getDocumentElement();
            NodeList ret = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < ret.getLength(); i++) {
                Object o = ret.item(i);
                if (o instanceof String) {
                    out_str.append(o);
                } else if (o instanceof Node) {
                    Node n = (Node) o;
                    short nt = n.getNodeType();
                    switch (nt) {
                    case Node.TEXT_NODE:
                    case Node.ATTRIBUTE_NODE:
                    case Node.CDATA_SECTION_NODE: // Only one value gets
                                                  // evaluated?
                        if (out_str.length() > 0)
                            out_str.append(',');
                        if (nt == Node.ATTRIBUTE_NODE)
                            out_str.append(n.getNodeValue());
                        else
                            out_str.append(n.getTextContent());
                        break;
                    case Node.ELEMENT_NODE:
                        rootElement.appendChild(outDoc.importNode(n, true));
                        break;
                    }
                }
            }
            if (out_str.length() > 0) {
                rootElement.setTextContent(out_str.toString());
                out_str.setLength(0);
            }
            out_str.append(Util.docToString(outDoc, true));
        } catch (Exception e) {
            // Thrown most likely because the given XPath evaluates to a
            // string
            out_str.append(expr.evaluate(doc));
        }
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    if (out_str.length() > 5 && out_str.substring(0, 5).equals("<?xml"))
        out_str.delete(0, out_str.indexOf(">") + 1);
    EditText etOutput = (EditText) findViewById(R.id.etOutput);
    etOutput.setText(out_str.toString());
    // tfOutputSize.setText(String.valueOf(xmlString.length()));
}