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

public static boolean isNodeTypeText(Node node) {
    return node != null && node.getNodeType() == Node.TEXT_NODE;
}

From source file:Main.java

static public String getFragmentText(DocumentFragment elm) {
    Node node = elm.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE)
        return node.getNodeValue();

    return null;/*w  ww  .jav a 2  s . c  om*/
}

From source file:Main.java

/**
 * Get the element text node.  Locate the text node and return it.  For example;
 * <element>this is the text node</element>
 *
 * @param element The element to get the text node for
 * @return The text node//from  w w w .  ja v a 2s . c  om
 */
public static Node getElementTextNode(Element element) {

    Node textNode = null;

    // go through each child element
    Node node;
    short nodeType;
    NodeList children = element.getChildNodes();
    for (int ii = 0; ii < children.getLength(); ii++) {
        node = children.item(ii);
        nodeType = node.getNodeType();
        if (nodeType == Node.TEXT_NODE) {
            textNode = node;
            break;
        } else if (nodeType == Node.CDATA_SECTION_NODE) {
            textNode = node;
            break;
        }
    }
    return (textNode);
}

From source file:Main.java

public static void stripWhitespace(Element element) {
    final NodeList childNodeList = element.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node node = childNodeList.item(i);
        switch (node.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            final String text = ((CharacterData) node).getData();
            if (WhitespacePattern.matcher(text).matches()) {
                element.removeChild(node);
                --i;//w  ww. j  a  v  a  2 s  . co  m
            }
            break;
        case Node.ELEMENT_NODE:
            stripWhitespace((Element) node);
            break;
        }
    }
}

From source file:Main.java

public static void getInnerText(StringBuilder sb, Element elt, String separator) {
    NodeList tns = elt.getChildNodes();
    for (int j = 0; j < tns.getLength(); j++) {
        Node tn = tns.item(j);//  w ww  . j a  v  a 2 s  . c o  m
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        } else if (tn.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(separator);
            getInnerText(sb, (Element) tn, separator);
            sb.append(separator);
        }
    }
}

From source file:Main.java

public static String getElementText(Element e) {
    if (e == null) {
        return ("");
    }//from   w w  w .  j  a  v a2 s  . c  om

    NodeList children = e.getChildNodes();

    if (children == null) {
        return ("");
    }

    StringBuffer text = new StringBuffer();

    int listLength = children.getLength();

    for (int i = 0; i < listLength; i++) {
        Node node = children.item(i);

        int nodeType = node.getNodeType();

        if ((nodeType == Node.TEXT_NODE) || (nodeType == Node.CDATA_SECTION_NODE)) {
            String nodeValue = node.getNodeValue();
            if (nodeValue != null) {
                text.append(nodeValue);
            }
        }
    }

    return (text.toString().trim());
}

From source file:Main.java

public static String getText(Element element) {
    StringBuffer buf = new StringBuffer();
    NodeList list = element.getChildNodes();
    boolean found = false;
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            buf.append(node.getNodeValue());
            found = true;/*  ww w . ja va2 s . com*/
        }
    }
    return found ? buf.toString() : null;
}

From source file:Main.java

public static String getContent(Node n) {
    assertNotNull("node == null", n);
    StringBuilder result = new StringBuilder();
    if (n.getNodeType() == Node.TEXT_NODE) {
        result.append(n.getNodeValue());
    }//from w w w  .j a v a  2s. c o m
    NodeList childNodes = n.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node n2 = childNodes.item(i);
        result.append(getContent(n2));
    }

    String tmp = result.toString();
    return tmp.replaceAll(CHAR_NBSP, " ");
}

From source file:Main.java

public static Object getContent(Element element) {
    NodeList nl = element.getChildNodes();
    StringBuffer content = new StringBuffer();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);//from   w  w  w.j av a 2  s .c o  m
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            return node;
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            content.append(node.getNodeValue());
            break;
        }
    }
    return content.toString().trim();
}

From source file:Main.java

public static String getText(final Node node) {
    final StringBuilder result = new StringBuilder();
    if (!node.hasChildNodes())
        return "";

    final NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        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));
        }/*from   w ww .j a va 2s . c  o  m*/
    }

    return result.toString();
}