Example usage for org.w3c.dom Node TEXT_NODE

List of usage examples for org.w3c.dom Node TEXT_NODE

Introduction

In this page you can find the example usage for org.w3c.dom Node TEXT_NODE.

Prototype

short TEXT_NODE

To view the source code for org.w3c.dom Node TEXT_NODE.

Click Source Link

Document

The node is a Text node.

Usage

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 www . j av a  2  s  .c  om*/
    Node child = node.getFirstChild();
    while (child != null) {
        getTextFromNode(child, buffer, addSpace);
        child = child.getNextSibling();
    }
}

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://  ww w  . j  a v a2 s.c o m
        return null;
    }
}

From source file:Main.java

public static final Node copyElement(Node e) throws UnsupportedDataTypeException {
    Node result = null;/* w  w w  . j  a  v a  2  s. c  o m*/

    switch (e.getNodeType()) {
    case Node.ELEMENT_NODE:
        result = document.createElement(e.getNodeName());

        for (int i = 0; i < e.getAttributes().getLength(); i++) {
            Attr attr = document.createAttribute(e.getAttributes().item(i).getNodeName());
            attr.setNodeValue(e.getAttributes().item(i).getNodeValue());
            result.getAttributes().setNamedItem(attr);
        }
        break;

    case Node.CDATA_SECTION_NODE:
        result = document.createCDATASection(((CDATASection) e).getData());
        break;
    case Node.TEXT_NODE:
        if (((Text) e).getTextContent().replaceAll("\t", "").trim() != "")
            result = document.createTextNode(((Text) e).getTextContent().replaceAll("\t", "").trim());
        break;

    default:
        throw new UnsupportedDataTypeException(new StringBuilder(e.getNodeType()).toString());
    }

    for (int i = 0; i < e.getChildNodes().getLength(); i++)
        result.appendChild(copyElement(e.getChildNodes().item(i)));

    return result;
}

From source file:Main.java

public static String getText(Node node)

{

    StringBuffer result = new StringBuffer();

    if (!node.hasChildNodes())

        return "";

    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++)

    {//from w ww  . j  ava  2s  .  c  o  m

        Node subnode = list.item(i);

        if (subnode.getNodeType() == Node.TEXT_NODE)

        {

            result.append(subnode.getNodeValue());

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

        {

            result.append(subnode.getNodeValue());

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

        {

            // Recurse into the subtree for text

            // (and ignore comments)

            result.append(getText(subnode));

        }

    }

    return result.toString();

}

From source file:Main.java

/**
 * Get textual content of a node(for text nodes only).
 * /*w ww  . j  a  va 2  s. c  o  m*/
 * @param node a text node.
 * @return node's textual content.
 */
public static String getNodeValue(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
        Node data = childNodes.item(x);
        if (data.getNodeType() == Node.TEXT_NODE)
            return data.getTextContent();
    }
    return "";
}

From source file:Main.java

public static String getTextTag(Node node, String tagName) {
    Element elem = (Element) node;
    NodeList list = elem.getElementsByTagName(tagName);
    if (list.getLength() == 0)
        return null;
    Node child = list.item(0).getFirstChild();
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
    return null;/*from   ww w.  j  a  v  a 2 s.  co m*/
}

From source file:Main.java

public static String getNormalizedText(Node node) {
    String res = "";
    if (node.hasChildNodes()) {
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
                res += nl.item(i).getNodeValue();
            } else {
                // ignore <SCRIPT> nodes ...
                if (!nl.item(i).getNodeName().equalsIgnoreCase("script"))
                    res += getNormalizedText(nl.item(i));
            }/*from ww  w.ja  v a  2s  .  c o m*/
        }
    }
    return res;
}

From source file:Main.java

private static String ensureExtractTextNodeValue(final Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        return node.getNodeValue();
    }//from   w  ww.  jav a  2  s.c  o  m
    throw new IllegalArgumentException("Node is not a text Node");
}

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());//from   w ww  .  ja  v  a 2 s  .  c o m
    } 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:Main.java

public static boolean setNodeValue(Node domNode, String string) {
    if (domNode == null)
        return false;

    short nodeType = domNode.getNodeType();

    switch (nodeType) {
    case Node.ELEMENT_NODE: {
        setElementText((Element) domNode, string);
        break;/*  w  ww  . j  a v  a2 s.  c o  m*/
    }
    case Node.ATTRIBUTE_NODE:
    case Node.TEXT_NODE: {
        domNode.setNodeValue(string);
        break;
    }
    case Node.PROCESSING_INSTRUCTION_NODE: {
        ((ProcessingInstruction) domNode).setData(string);
        break;
    }
    case Node.CDATA_SECTION_NODE: {
        ((CDATASection) domNode).setData(string);
        break;
    }
    default: {
        return false;
    }
    }

    return true;
}