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 String get_inner_text(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE)
            return child.getNodeValue();
        if (child instanceof Text)
            return ((Text) child).getData();
        /*if (child instanceof TextNode)
           return ((TextNode)child).getData();*/
    }//from w ww .j av  a 2  s  .  co  m
    return null;
}

From source file:Utils.java

/**
 * //  w w w  .  j a va2  s  .  c o  m
 */
public static String getElementText(Element element) {
    StringBuffer buffer = new StringBuffer();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
            buffer.append(node.getNodeValue());
        }
    }
    return buffer.toString();
}

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();
            }/*  w w w . jav  a 2s.  com*/
        }
    }
    return null;
}

From source file:Main.java

public static String getFirstLevelTextContent(Node node) {
    NodeList list = node.getChildNodes();
    StringBuilder textContent = new StringBuilder();
    for (int i = 0; i < list.getLength(); ++i) {
        Node child = list.item(i);
        if (child.getNodeType() == Node.TEXT_NODE)
            textContent.append(child.getTextContent());
    }/*from ww  w .  j  ava 2  s  .  c om*/
    return textContent.toString().trim();
}

From source file:Main.java

/**
 * Returns the text associated to the given node
 * i.e. the value of the//from ww  w  .  jav  a2  s  .c o  m
 * @param node
 * @return
 */
public static String getText(Node node) {
    Node textNode = node.getFirstChild();
    if (textNode == null) {
        return null;
    }
    short nodeType = textNode.getNodeType();
    if (nodeType != Node.TEXT_NODE && nodeType != Node.CDATA_SECTION_NODE) {
        return null;
    }
    return textNode.getNodeValue().trim();
}

From source file:Main.java

private static ArrayList<SimpleEntry<String, String>> traverseNode(Node n, String p) {
    ArrayList<SimpleEntry<String, String>> output = new ArrayList<>();
    String nName;/*from ww  w . jav  a 2 s.  co  m*/
    if (n.getNodeType() != Node.TEXT_NODE) {
        nName = n.getNodeName();
        if (nName.startsWith("m:"))
            nName = nName.substring(2);
        if (nName.equals("mws:qvar"))
            return new ArrayList<>();
        p += "/" + nName;
    }
    String nValue = n.getNodeValue();
    if (nValue != null) {
        nValue = nValue.trim();
        if (nValue.length() == 0) {
            return new ArrayList<>();
        }
    } else {
        nValue = "";
    }

    if (!n.hasChildNodes()) {
        output.add(new SimpleEntry<>(p, nValue));
    } else {
        for (int i = 0; i < n.getChildNodes().getLength(); i++) {
            output.addAll(traverseNode(n.getChildNodes().item(i), p));
        }
    }
    return output;
}

From source file:Main.java

public static Set<Node> getChildrenElements(Node node) {
    final Set<Node> result = new LinkedHashSet<>();

    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() != Node.TEXT_NODE) {
            result.add(child);//from   w  w w.j  av  a2s  .c  o m
        }
    }

    return result;
}

From source file:Main.java

public static Node getTextNode(Node node) {
    Node textnode = null;//from w ww .  j  a va  2s .  c o  m
    NodeList nodes = node.getChildNodes();
    int len = nodes.getLength();
    if (len > 0) {
        for (int i = 0; i < len; i++) {
            if (nodes.item(i).getNodeType() == Node.TEXT_NODE) {
                textnode = nodes.item(i);
                break;
            }
        }
    }
    return textnode;
}

From source file:Main.java

private static Object nodeValue(Node node) {
    NodeList childList = node.getChildNodes();
    if (childList.getLength() == 0) {
        return "";
    } else if (childList.getLength() == 1 && childList.item(0).getNodeType() == Node.TEXT_NODE) {
        return node.getTextContent().trim();
    } else {/*w w w .java  2  s.com*/
        List<Object> value = new ArrayList<Object>();
        for (int i = 0; i < childList.getLength(); i++) {
            value.add(nodeValue(childList.item(i)));
        }
        return value;
    }
}

From source file:Main.java

/**
 * Returns element's TEXT Node/* ww w.  j av a 2 s.c  o  m*/
 * 
 * @param element
 *            the element which TEXT node is returned
 * @return TEXT node
 */
public static Text getElementTextNode(Element element) {
    return (Text) getChildNodeByType(element, Node.TEXT_NODE);
}