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

/**
 * Method getStrFromNode/*from  w  ww  . j av a2 s.  c o  m*/
 *
 * @param xpathnode
 * @return the string for the node.
 */
public static String getStrFromNode(Node xpathnode) {
    if (xpathnode.getNodeType() == Node.TEXT_NODE) {
        // we iterate over all siblings of the context node because eventually,
        // the text is "polluted" with pi's or comments
        StringBuilder sb = new StringBuilder();

        for (Node currentSibling = xpathnode.getParentNode()
                .getFirstChild(); currentSibling != null; currentSibling = currentSibling.getNextSibling()) {
            if (currentSibling.getNodeType() == Node.TEXT_NODE) {
                sb.append(((Text) currentSibling).getData());
            }
        }

        return sb.toString();
    } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
        return xpathnode.getNodeValue();
    } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
        return xpathnode.getNodeValue();
    }

    return null;
}

From source file:Main.java

public static String getTextContent(Node node, boolean trim) {
    if (node == null) {
        return "";
    }//from w  ww .  ja  va 2  s .c  o m

    String textContent = "";
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            textContent += childNode.getNodeValue().trim();
        }
    }

    textContent = textContent.replace("\r", "");
    if (textContent.startsWith("\n")) {
        textContent = textContent.substring(1);
    }

    if (trim) {
        textContent = textContent.trim();
    }

    return textContent;
}

From source file:Main.java

/**
 * Returns the content of a simple text tag If there are more than one text node or other nodetypes it will return null.
 *
 * @param node// w  ww .  j  a va 2  s  . c  o m
 *            the node
 * @return the simple text value from node
 */
public static String getSimpleTextValueFromNode(Node node) {
    if (node != null && node.getChildNodes().getLength() == 1
            && node.getChildNodes().item(0).getNodeType() == Node.TEXT_NODE) {
        return node.getChildNodes().item(0).getTextContent();
    } else {
        return null;
    }
}

From source file:Main.java

/**
 * Get element node string value./*from   ww  w .j a  v  a2  s .  com*/
 * 
 * @param element element to get string value for
 * @return concatenated text node descendant values
 */
public static String getStringValue(final Element element) {
    final StringBuilder buf = new StringBuilder();
    final NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        final Node n = children.item(i);
        switch (n.getNodeType()) {
        case Node.TEXT_NODE:
            buf.append(n.getNodeValue());
            break;
        case Node.ELEMENT_NODE:
            buf.append(getStringValue((Element) n));
            break;
        }
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Gets the nodenames of childs.//from  w w  w  . j av a  2 s .  c om
 *
 * @param dataNode
 *            the data node
 * @return the nodenames of childs
 */
public static List<String> getNodenamesOfChilds(Node dataNode) {
    List<String> returnList = new ArrayList<String>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() != Node.TEXT_NODE) {
            returnList.add(node.getNodeName());
        }
    }

    return returnList;
}

From source file:Main.java

/**
 * Gets the text value of the given element.
 * /*from  www  .j  a va  2 s . c om*/
 * @param e the element.
 * @return the text contents of the given element.s
 */
public static String getText(Element e) {
    StringBuilder sb = null;

    if (e != null) {
        NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);

            switch (node.getNodeType()) {
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                if (sb == null) {
                    sb = new StringBuilder();
                }
                sb.append(node.getNodeValue());
                break;
            }
        }
    }

    return (sb != null) ? sb.toString() : null;
}

From source file:Main.java

/**
 * Collapses a list of CDATASection, Text, and predefined EntityReference
 * nodes into a single string.  If the list contains other types of nodes,
 * those other nodes are ignored./*  w ww . j a v  a 2 s  .  c  om*/
 */
public static String getText(NodeList nodeList) {
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        switch (node.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            buffer.append(node.getNodeValue());
            break;
        case Node.ENTITY_REFERENCE_NODE:
            if (node.getNodeName().equals("amp"))
                buffer.append('&');
            else if (node.getNodeName().equals("lt"))
                buffer.append('<');
            else if (node.getNodeName().equals("gt"))
                buffer.append('>');
            else if (node.getNodeName().equals("apos"))
                buffer.append('\'');
            else if (node.getNodeName().equals("quot"))
                buffer.append('"');
            // Any other entity references are ignored
            break;
        default:
            // All other nodes are ignored
        }
    }
    return buffer.toString();
}

From source file:Main.java

/**
 * Prints out the DOM tree./*from w ww  .  j  ava  2s  .c  om*/
 * 
 * @param n
 *            the parent node to start printing from
 * @param prefix
 *            string to append to output, should not be null
 */
public static void printDom(Node n, String prefix, StringBuilder sb) {
    String outString = prefix;
    if (n.getNodeType() == Node.TEXT_NODE) {
        outString += "'" + n.getTextContent().trim() + "'";
    } else {
        outString += n.getNodeName();
    }
    sb.append(outString);
    sb.append("\n");
    NodeList children = n.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        printDom(children.item(i), prefix + "  ", sb);
    }
}

From source file:Utils.java

/**
 * Get the raw text content of a node or null if there is no text
 *//*  w  w  w  . jav  a  2  s.com*/
public static String getRawContent(Node n) {
    if (n == null) {
        return null;
    }

    Node n1 = getChild(n, Node.TEXT_NODE);

    if (n1 == null) {
        return null;
    }

    return n1.getNodeValue();
}

From source file:Main.java

public static String getChildNodeValue(String nodeName, Node parent) {

    if (parent == null) {
        return null;
    }/*from   ww  w  . ja va 2s.c om*/

    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(nodeName)) {
                Node firstChild = node.getFirstChild();
                if ((firstChild != null) && (firstChild.getNodeType() == Node.TEXT_NODE)) {
                    return firstChild.getNodeValue();
                } else {
                    return null;
                }
            }
        }
    }

    return null;
}