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

public static boolean isText(Node node) {
    return node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE;
}

From source file:Utils.java

public static String getElementText(Element e) {
    StringBuffer buf = new StringBuffer();
    for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
            buf.append(n.getNodeValue());
        }//from  w  w w  . java2s  .c om
    }
    return buf.toString();
}

From source file:Main.java

/** Decide if the node is text, and so must be handled specially */
public static boolean isTextNode(final Node n) {
    if (n == null) {
        return false;
    }/*from   w w w .  ja  v a 2s  .c o  m*/
    short nodeType = n.getNodeType();
    return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE;
}

From source file:Main.java

public static final String getText(Node node) {
    if (node.hasChildNodes()) {
        NodeList childNodes = node.getChildNodes();
        if (childNodes.getLength() > 0) {
            Node child = childNodes.item(0);
            if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
                return child.getNodeValue();
            }//from  w  w w  . j  ava 2  s. c  om
        }
    }
    return null;
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node == null)
        return null;
    StringBuffer buff = new StringBuffer();
    for (int c = 0; c < node.getChildNodes().getLength(); c++) {
        Node cn = node.getChildNodes().item(c);
        if (cn.getNodeType() == Node.TEXT_NODE || cn.getNodeType() == Node.CDATA_SECTION_NODE) {
            buff.append(cn.getNodeValue());
        }/*from  ww  w  .ja  v a  2  s  .c  om*/
    }
    return buff.toString().trim();

}

From source file:Main.java

public static String getValue(Element element) {
    NodeList l = element.getChildNodes();
    StringBuffer s = new StringBuffer();
    for (int i = 0; i < l.getLength(); i++) {
        switch (l.item(i).getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            s.append(l.item(i).getNodeValue());
        }//from w w w. j a  v a 2 s.  com
    }
    return s.toString();
}

From source file:Main.java

/**
 * Method to convert a NODE XML to a string.
 * @param n node XML to input.//from w  w w .j  a  v a  2 s . com
 * @return string of the node n.
 */
public static String convertElementToString(Node n) {
    String name = n.getNodeName();
    short type = n.getNodeType();
    if (Node.CDATA_SECTION_NODE == type) {
        return "<![CDATA[" + n.getNodeValue() + "]]&gt;";
    }
    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;
    NodeList children = n.getChildNodes();
    if (children.getLength() == 0) {
        if ((textContent = n.getTextContent()) != 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 = convertElementToString(children.item(i));
            if (!"".equals(childToString)) {
                sb.append(childToString);
                hasValidChildren = true;
            }
        }
        if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) {
            sb.append(textContent);
        }
        sb.append("</").append(name).append('>');
    }
    return sb.toString();
}

From source file:Main.java

public static void setCDATAContent(Node node, String value) {
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node child = nodeList.item(i);
            if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
                CDATASection cdata = (CDATASection) child;
                cdata.setData(value);/*from  w w  w.  j  a v  a 2s. c o  m*/
            }
        }
    }
}

From source file:Main.java

public static String path(Node n) {
    if (n == null)
        return "";
    switch (n.getNodeType()) {
    case Node.ATTRIBUTE_NODE: {
        return path(n.getParentNode()) + "/@" + n.getNodeName();
    }// w  w  w.  ja  v  a2 s .  co  m
    case Node.TEXT_NODE: {
        return path(n.getParentNode()) + "/text()";
    }
    case Node.CDATA_SECTION_NODE: {
        return path(n.getParentNode()) + "/cdata()";
    }
    case Node.ELEMENT_NODE: {
        return path(n.getParentNode()) + "/" + n.getNodeName();
    }
    case Node.DOCUMENT_NODE: {
        return "";
    }
    default: {
        return "";
    }
    }
}

From source file:Main.java

public static String getTextData(Node node) {
    if (!node.hasChildNodes()) {
        return null;
    }//from   w  w w . j  av  a2  s .c  o m
    Node child = node.getFirstChild();
    while (child != null && child.getNodeType() != Node.TEXT_NODE
            && child.getNodeType() != Node.CDATA_SECTION_NODE) {
        child = child.getNextSibling();
    }
    if (child == null) {
        return null;
    }

    if (child.getNodeType() == Node.TEXT_NODE) {
        return ((Text) child).getData();
    } else {
        return ((CDATASection) child).getData();
    }

}