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

/** Prints the specified node, recursively.
 *
 *  @param node Node to be printed// w w w . ja  va2  s  .c  om
 */
public static void print(Node node) {
    // is there anything to do?
    if (node == null) {
        return;
    }
    System.out.println("");

    int type = node.getNodeType();
    switch (type) {
    // print document
    case Node.DOCUMENT_NODE: {
        /*
        if (!canonical) {
          if (Encoding.equalsIgnoreCase("DEFAULT"))
        Encoding = "UTF-8";
          else if(Encoding.equalsIgnoreCase("Unicode"))
        Encoding = "UTF-16";
          else 
        Encoding = MIME2Java.reverse(Encoding);
        out.println("<?xml version=\"1.0\" encoding=\"" +
          Encoding + "\"?>");
        }
         */
        print(((Document) node).getDocumentElement());
        out.flush();
        break;
    }
    // print element with attributes
    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        Attr attrs[] = sortAttributes(node.getAttributes());
        for (int i = 0; i < attrs.length; i++) {
            Attr attr = attrs[i];
            out.print(' ');
            out.print(attr.getNodeName());
            out.print("=\"");
            out.print(normalize(attr.getNodeValue()));
            out.print('"');
        }
        out.print('>');
        NodeList children = node.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                print(children.item(i));
            }
        }
        break;
    }
    // handle entity reference nodes
    case Node.ENTITY_REFERENCE_NODE: {
        if (canonical) {
            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++) {
                    print(children.item(i));
                }
            }
        } else {
            out.print('&');
            out.print(node.getNodeName());
            out.print(';');
        }
        break;
    }
    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        if (canonical) {
            out.print(normalize(node.getNodeValue()));
        } else {
            out.print("<![CDATA[");
            out.print(node.getNodeValue());
            out.print("]]>");
        }
        break;
    }
    // print DocumentType sections
    case Node.DOCUMENT_TYPE_NODE: {
        out.print("<!DOCTYPE ");
        out.print(((DocumentType) node).getName());
        out.print(" SYSTEM ");
        out.print(((DocumentType) node).getSystemId());
        out.print(">");
        break;
    }
    // print text
    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    // print processing instruction
    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String data = node.getNodeValue();
        if (data != null && data.length() > 0) {
            out.print(' ');
            out.print(data);
        }
        out.print("?>");
        break;
    }
    }
    if (type == Node.ELEMENT_NODE) {
        out.print("</");
        out.print(node.getNodeName());
        out.print('>');
    }
    out.flush();
}

From source file:com.jaspersoft.jasperserver.ws.xml.Unmarshaller.java

static public String readPCDATA(Node textNode, boolean trim) {
    NodeList list_child = textNode.getChildNodes();
    for (int ck = 0; ck < list_child.getLength(); ck++) {
        Node child_child = (Node) list_child.item(ck);

        // --- start solution: if there is another node this should be the PCDATA-node
        Node ns = child_child.getNextSibling();
        if (ns != null)
            child_child = ns;// w w w.  ja v  a2  s.com
        // --- end solution

        final short nt = child_child.getNodeType();

        // 1. look for a CDATA first...
        if (nt == Node.CDATA_SECTION_NODE) {
            if (trim)
                return ((String) child_child.getNodeValue()).trim();
            return (String) child_child.getNodeValue();
        }
    }

    for (int ck = 0; ck < list_child.getLength(); ck++) {
        Node child_child = (Node) list_child.item(ck);

        // --- start solution: if there is another node this should be the PCDATA-node
        Node ns = child_child.getNextSibling();
        if (ns != null)
            child_child = ns;
        // --- end solution

        final short nt = child_child.getNodeType();
        // 1. look for a CDATA first...
        if (nt == Node.TEXT_NODE) {
            if (trim)
                return ((String) child_child.getNodeValue()).trim();
            return (String) child_child.getNodeValue();
        }
    }

    return "";
}

From source file:Main.java

/**
 * Looks for a text child node and returns its value.
 *
 * @param tag - XML element//from ww  w.j  a  va  2s  . co  m
 * @return - the text String of the tag
 */
public static String getText(final Element tag) {
    if (tag == null)
        return null;

    NodeList lst = tag.getChildNodes();
    StringBuffer buf = new StringBuffer();

    for (int Index = 0, Cnt = lst.getLength(); Index < Cnt; Index++) {
        if (lst.item(Index).getNodeType() == Node.ENTITY_REFERENCE_NODE) {
            buf.append(lst.item(Index).getChildNodes().item(0).getNodeValue());
        }
        if ((lst.item(Index).getNodeType() == Node.TEXT_NODE)
                || (lst.item(Index).getNodeType() == Node.CDATA_SECTION_NODE)) {
            buf.append(lst.item(Index).getNodeValue());
        }
    }

    if (buf.length() == 0)
        return null;
    else
        return buf.toString();
}

From source file:dk.statsbiblioteket.util.xml.DOM.java

/**
 * Extracts all textual and CDATA content from the given node and its
 * children.//from  w w w  .j a v  a 2  s.c om
 *
 * @param node the node to get the content from.
 * @return the textual content of node.
 */
public static String getElementNodeValue(Node node) {
    StringWriter sw = new StringWriter(2000);
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        NodeList all = node.getChildNodes();
        for (int i = 0; i < all.getLength(); i++) {
            if (all.item(i).getNodeType() == Node.TEXT_NODE
                    || all.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
                // TODO: Check if we exceed the limit for getNodeValue
                sw.append(all.item(i).getNodeValue());
            }
        }
    }
    return sw.toString();
}

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;//  ww  w  . j a  va 2  s  .c  o  m
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        //out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}

From source file:Main.java

/**prints the type of the input node
 * @param node node to print type of/*from ww  w  .  j  av  a2 s .  c om*/
 * @param ident amount to indent*/
public static void printNodeType(Node node, int ident) {
    System.out.print("Node: " + node.getNodeName() + " ");
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node");
        break;

    case Node.ELEMENT_NODE:
        System.out.println("Element Node");
        for (int j = 0; j < 2 * ident; j++)
            System.out.print(" ");
        System.out.println("It has the following Children");
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                for (int j = 0; j < ident; j++)
                    System.out.print(" ");
                System.out.print("Child " + ident + "." + i + " = ");
                printNodeType(children.item(i), ident + 1);
            }
            System.out.println();
        }
        break;

    case Node.TEXT_NODE:
        System.out.println("->" + node.getNodeValue().trim() + "<-");
        break;

    case Node.CDATA_SECTION_NODE:
        System.out.println("CData Node");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Proposing Instruction Node");
        break;

    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Node");
        break;

    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Node");
        break;

    default:
    }
}

From source file:com.microsoft.tfs.util.xml.DOMUtils.java

/**
 * A DOM helper method to get the text contents of a {@link Node}. If the
 * {@link Node} is a text node ({@link Node#TEXT_NODE}) or a CDATA section
 * node ({@link Node#CDATA_SECTION_NODE}), the node's value is returned. If
 * the {@link Node} is a element node ({@link Node#ELEMENT_NODE}), the node
 * value of any <i>direct child</i> nodes that are text or CDATA sections
 * are appended together and returned. If the node is not of any of the
 * above types, an empty string is returned.
 *
 * @param node/*from  w  ww  .  j ava 2 s.  c  om*/
 *        the {@link Node} to get text for (must not be <code>null</code>)
 * @return the text (never <code>null</code> but may be an empty
 *         {@link String})
 */
public static String getText(final Node node) {
    Check.notNull(node, "node"); //$NON-NLS-1$

    final int type = node.getNodeType();

    if (Node.ELEMENT_NODE == type) {
        final NodeList children = node.getChildNodes();
        final StringBuffer buffer = new StringBuffer();
        final int length = children.getLength();
        for (int i = 0; i < length; i++) {
            final Node child = children.item(i);
            final int childType = child.getNodeType();
            if (Node.TEXT_NODE == childType || Node.CDATA_SECTION_NODE == childType) {
                buffer.append(child.getNodeValue());
            }
        }
        return buffer.toString();
    }

    if (Node.TEXT_NODE == type || Node.CDATA_SECTION_NODE == type) {
        return node.getNodeValue();
    }

    return ""; //$NON-NLS-1$
}

From source file:Main.java

/**
 * Locate the first text node at any level below the given node. If the
 * ignoreEmpty flag is true, we will ignore text nodes that contain only
 * whitespace characteres./*from   w w w  .  j  a v a 2 s .  c  o m*/
 * <p/>
 * Note that if you're trying to extract element content, you probably don't
 * want this since parser's can break up pcdata into multiple adjacent text
 * nodes. See getContent() for a more useful method.
 */
private static Text findText(Node node, boolean ignoreEmpty) {

    Text found = null;

    if (node != null) {

        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {

            Text t = (Text) node;
            if (!ignoreEmpty) {
                found = t;
            } else {
                String s = t.getData().trim();
                if (s.length() > 0) {
                    found = t;
                }
            }
        }

        if (found == null) {

            for (Node child = node.getFirstChild(); child != null
                    && found == null; child = child.getNextSibling()) {

                found = findText(child, ignoreEmpty);
            }
        }
    }

    return found;
}

From source file:Main.java

/**
 * Returns String representation of specified xml node.
 *
 * @param node   the specified xml node// w ww  . ja  v a  2 s .c om
 * @return string representation of the node
 */
public static String getNodeData(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_FRAGMENT_NODE:
    case Node.DOCUMENT_NODE:
    case Node.ELEMENT_NODE: {
        /*for (Node child = node.getFirstChild(); null != child;
          child = child.getNextSibling())*/
        Node child = node.getFirstChild();
        if (child != null)
            return getNodeData(child);
    }
        break;
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
        return node.getNodeValue();
    case Node.ATTRIBUTE_NODE:
        return node.getNodeValue();
    case Node.PROCESSING_INSTRUCTION_NODE:
        break;
    default:
        break;
    }
    return "";
}

From source file:MainClass.java

private void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;//from   w  w w. j a v a  2 s .  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.println(indent + "TEXT_NODE");
        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 + "   ");
}