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

/**
 * Gets Node type String for a given node type constant
 */// w w  w.j  a va2s  . c om

public static String getNodeTypeStr(int nodeType)

{

    switch (nodeType)

    {

    case Node.ATTRIBUTE_NODE:

        return "ATTRIBUTE_NODE ";

    case Node.CDATA_SECTION_NODE:

        return "CDATA_SECTION_NODE";

    case Node.COMMENT_NODE:

        return "COMMENT_NODE";

    case Node.DOCUMENT_FRAGMENT_NODE:

        return "DOCUMENT_FRAGMENT_NODE";

    case Node.DOCUMENT_TYPE_NODE:

        return "DOCUMENT_TYPE_NODE";

    case Node.ELEMENT_NODE:

        return "ELEMENT_NODE";

    case Node.ENTITY_NODE:

        return "ENTITY_NODE";

    case Node.ENTITY_REFERENCE_NODE:

        return "ENTITY_REFERENCE_NODE";

    case Node.NOTATION_NODE:

        return "NOTATION_NODE";

    case Node.PROCESSING_INSTRUCTION_NODE:

        return "PROCESSING_INSTRUCTION_NODE";

    case Node.TEXT_NODE:

        return "TEXT_NODE";

    case Node.DOCUMENT_NODE:

        return "DOCUMENT_NODE";

    default:

        return "UN-INDENTIFIED NODE";

    }

}

From source file:DOMDump.java

private static void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;//from w ww.  ja  va  2s. c o m
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.print(indent + "TEXT_NODE");
        System.out.println(" : " + node.getTextContent());
        break;
    default:
        System.out.println(indent + "Unknown node");
        break;
    }

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        dumpLoop(list.item(i), indent + "   ");
    }
}

From source file:Main.java

static void printElement(Element element, String indent) {
    System.out.println("Element '" + element.getNodeName() + "'");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE:
            printElement((Element) child, indent + "\t");
            break;
        case Node.ATTRIBUTE_NODE:
            Attr attr = (Attr) child;
            System.out.println("\tAttribute: '" + attr.getName() + "' = '" + attr.getValue() + "'");
            break;
        case Node.COMMENT_NODE:
            Comment comment = (Comment) child;
            System.out.println("\tComment: '" + comment.getData() + "'");
            break;
        case Node.CDATA_SECTION_NODE:
            CharacterData cdata = (CharacterData) child;
            System.out.println("\tCDatat: '" + cdata.getData() + "'");
            break;
        case Node.TEXT_NODE:
            Text text = (Text) child;
            System.out.println("\tText: '" + text.getData() + "'");
            break;
        default://from w ww  .  j  av  a 2  s  .  c om
            System.out.println("\tUnknown node type: '" + child.getNodeType() + "'");
            break;
        }
    }
}

From source file:Main.java

/**
 * Returns the concatenated child text of the specified node.
 * This method only looks at the immediate children of type
 * Node.TEXT_NODE or the children of any child
 * node that is of type Node.CDATA_SECTION_NODE
 * for the concatenation.//from   www. j  av  a 2  s . c  o m
 *
 * @param node The node to look at.
 */
public static String getChildText(Node node) {

    // is there anything to do?
    if (node == null) {
        return null;
    }

    // concatenate children text
    StringBuffer str = new StringBuffer();
    Node child = node.getFirstChild();
    while (child != null) {
        short type = child.getNodeType();
        if (type == Node.TEXT_NODE) {
            str.append(child.getNodeValue());
        } else if (type == Node.CDATA_SECTION_NODE) {
            str.append(getChildText(child));
        }
        child = child.getNextSibling();
    }

    // return text value
    return str.toString();

}

From source file:Main.java

public static String getElementText(Element e) {
    final StringBuilder val = new StringBuilder();
    for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
            val.append(n.getNodeValue());
        }/*www. j  a v  a2s  .  co  m*/
    }
    return val.toString();
}

From source file:Main.java

/**
 * _more_//from  www  . j a  v  a  2s.c  o  m
 *
 * @param html _more_
 * @param node _more_
 */
public static void toHtml(StringBuffer html, Node node) {
    switch (node.getNodeType()) {

    case Node.ELEMENT_NODE: {
        NodeList children = node.getChildNodes();
        int numChildren = children.getLength();
        html.append("<b>" + node.getNodeName().replace("_", " ") + "</b>");
        html.append(": ");

        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) {
                String v = child.getNodeValue();
                if (v == null) {
                    continue;
                }
                if (v.trim().length() == 0) {
                    continue;
                }
                html.append(v);
                html.append(" ");
            }
        }
        boolean didone = false;
        NamedNodeMap nnm = node.getAttributes();
        if (nnm != null) {
            for (int i = 0; i < nnm.getLength(); i++) {
                Attr attr = (Attr) nnm.item(i);
                String attrName = attr.getNodeName();
                if (attrName.startsWith("xmlns") || attrName.startsWith("xsi:")) {
                    continue;
                }
                if (!didone) {
                    html.append("<ul>");
                    didone = true;
                }
                html.append(attrName.replace("_", " ") + "=" + attr.getNodeValue());
                html.append("<br>\n");
            }
        }
        int cnt = 0;
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (((child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE))) {
                continue;
            }
            if (!didone) {
                html.append("<ul>");
                didone = true;
            }
            if (cnt > 0) {
                html.append("<br>");
            }
            toHtml(html, child);
            cnt++;
        }
        if (didone) {
            html.append("</ul>");
        }
        break;
    }
    }
}

From source file:Main.java

static StringBuffer textContent(Node node, StringBuffer s) {
    if (node == null)
        return s;
    for (Node c = node.getFirstChild(); c != null; c = c.getNextSibling()) {
        if (c.getNodeType() == Node.CDATA_SECTION_NODE) {
            s.append(((CDATASection) c).getNodeValue());
        } else if (c.getNodeType() == Node.TEXT_NODE) {
            s.append(((Text) c).getNodeValue());
        } else {// w  w  w  . ja  v  a 2  s  .c  om
            textContent(c, s);
        }
    }
    return s;
}

From source file:Main.java

private 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:/*  w  w w.  j a  v  a2  s  .  com*/
        return null;
    }
}

From source file:Main.java

/**
 * Clones the given DOM node into the given DOM document.
 * //  w  w w .j a  v a  2s .  co m
 * @param node
 *            The DOM node to clone.
 * @param doc
 *            The target DOM document.
 * @return The cloned node in the target DOM document.
 */
public static Node cloneNode(Node node, Document doc) {
    Node clone = null;
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        clone = doc.createElement(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attrNode = attrs.item(i);
            Attr attrClone = doc.createAttribute(attrNode.getNodeName());
            attrClone.setNodeValue(attrNode.getNodeValue());
            ((Element) clone).setAttributeNode(attrClone);
        }

        // Iterate through each child nodes.
        NodeList childNodes = node.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node childNode = childNodes.item(i);
                if (childNode == null) {
                    continue;
                }
                Node childClone = cloneNode(childNode, doc);
                if (childClone == null) {
                    continue;
                }
                clone.appendChild(childClone);
            }
        }
        break;

    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        clone = doc.createTextNode(node.getNodeName());
        clone.setNodeValue(node.getNodeValue());
        break;
    }
    return clone;
}

From source file:Main.java

/**
 * Checks if a node has contents.//from w  ww  . j  av a 2s.  c  o  m
 * A node is considered as <i>having content</i> when:
 * <ul>
 *       <li>When the node or any of its childs contains a non-empty attribute.</li>
 *       <li>When the node or any of its childs contains non-empty text nodes. If the 
 *       parameter <b>ignoreBlankSpaces</b> is <i>true</i>, text nodes containing
 *       only control characters (ascii codes equal or smaller than 32) are considered
 *       empty.</li>
 * </ul>
 * Comment and processing instruction nodes are not considered as content.
 * @param x The node to check. If it is null, the method returns <i>false</i>.
 * @param ignoreBlankSpaces If <i>true</i> text nodes containing
 * only control characters (ascii codes equal or smaller than 32) are considered
 * empty.
 * @return <i>true</i> if the specified node has contents.
 */
static public boolean NodeHasContent(Node x, boolean ignoreBlankSpaces) {
    int n, nn; // To parse child nodes of "x".
    Node child; // One of the childs of "x".
    int childType; // Type of the child node.
    String childContent; // When the child is a text node, its content.

    //.............................. Looks for "non-empty" nodes ...................
    if (x != null) {
        nn = x.getChildNodes().getLength();
        for (n = 0; n < nn; n++) {
            child = x.getChildNodes().item(n);
            childType = child.getNodeType();
            switch (childType) {
            // Nodes containing text:
            case Node.CDATA_SECTION_NODE:
            case Node.TEXT_NODE:
                if (ignoreBlankSpaces)
                    childContent = child.getNodeValue().trim();
                else
                    childContent = child.getNodeValue();
                if (childContent.length() > 0)
                    return true;

                // Elements (nodes containing nodes):
            case Node.ELEMENT_NODE:
                if (NodeHasContent(child, ignoreBlankSpaces))
                    return true;

                // Any other type of node are not considered as content:
            default:
                continue;
            }
        }
    }
    //............................. If no "non-empty" node has been found ..........
    return false;
}