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 getTextContent(Node node) {
    StringBuffer buffer = new StringBuffer();
    NodeList childList = node.getChildNodes();
    for (int i = 0; i < childList.getLength(); i++) {
        Node child = childList.item(i);
        if (child.getNodeType() != Node.TEXT_NODE)
            continue; // skip non-text nodes
        buffer.append(child.getNodeValue());
    }//from  ww w.j  ava  2 s  .c om
    return buffer.toString();
}

From source file:Main.java

/**
 * Prints a textual representation of the given node to the specified PrintStream.
 *
 * @param  n    Node that is to be printed.
 * @param  out  The PrintStream to which the node is to be printed.
 * @pre    n != null && out != null
 *//* w w w  .  j av  a  2s .  c  om*/
public static void printXMLNode(Node n, PrintStream out) {
    switch (n.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.println("DOC_ROOT");
        break;

    case Node.ELEMENT_NODE:
        out.println("<" + ((Element) n).getTagName() + ">");
        break;

    case Node.ATTRIBUTE_NODE:
        out.println("@" + ((Attr) n).getName());
        break;

    case Node.TEXT_NODE:
        out.println("\"" + ((Text) n).getWholeText().trim() + "\"");
        break;

    case Node.COMMENT_NODE:
        out.println("COMMENT: \"" + n.getTextContent().trim() + "\"");
        break;

    default:
        out.println("Unknown node type: " + n.getNodeType());
    }
}

From source file:Main.java

/**
 * Utility method to fetch the value of the element node
 * @param node//from   w  w w  .ja va 2  s  .c  o m
 * @return
 */
public static String getNodeValue(Node node) {
    for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.TEXT_NODE) {
            return child.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Recursively removes all text nodes from a DOM element.
 * //from   w ww .  j av a 2s.c o  m
 * @param element
 *        The element
 */
public static void removeTextNodes(Element element) {
    Node nextNode = element.getFirstChild();
    for (Node child = element.getFirstChild(); nextNode != null;) {
        child = nextNode;
        nextNode = child.getNextSibling();
        if (child.getNodeType() == Node.TEXT_NODE)
            element.removeChild(child);
        else if (child.getNodeType() == Node.ELEMENT_NODE)
            removeTextNodes((Element) child);
    }
}

From source file:Main.java

/**
 * Returns all direct simple text value subnodes and their values for a dataNode
 *
 * Example XML: <dataNode> <a>1</a> <b>2</b> <c>3</c> </dataNode> Returns: a=1, b=2, c=3.
 *
 * @param dataNode//from w  ww  . ja va2  s  . c  o  m
 *            the data node
 * @return the simple values of node
 */
public static Map<String, String> getSimpleValuesOfNode(Node dataNode) {
    Map<String, String> returnMap = new HashMap<String, String>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.TEXT_NODE
                && node.getChildNodes().getLength() == 1) {
            returnMap.put(node.getNodeName(), node.getFirstChild().getNodeValue());
        }
    }

    return returnMap;
}

From source file:Main.java

/**
 * Trim all new lines from text nodes./*from  w w  w  . ja v a 2s . c  o  m*/
 *
 * @param node
 */
public static void normalize(Node node) {
    if (node.getNodeType() == Node.TEXT_NODE) {
        String data = ((Text) node).getData();
        if (data.length() > 0) {
            char ch = data.charAt(data.length() - 1);
            if (ch == '\n' || ch == '\r' || ch == ' ') {
                String data2 = trim(data);
                ((Text) node).setData(data2);
            }
        }
    }
    for (Node currentChild = node.getFirstChild(); currentChild != null; currentChild = currentChild
            .getNextSibling()) {
        normalize(currentChild);
    }
}

From source file:Main.java

/**
 * return the text content of an element
 *//*from   w w w . ja va 2  s .c  o m*/
public static String getTextContent(org.w3c.dom.Node element) {
    StringBuffer childtext = new StringBuffer();
    NodeList childlist = element.getChildNodes();
    int ct = childlist.getLength();

    for (int j = 0; j < ct; j++) {
        org.w3c.dom.Node childNode = childlist.item(j);

        if ((childNode.getNodeType() == Node.TEXT_NODE)
                || (childNode.getNodeType() == Node.CDATA_SECTION_NODE)) {
            childtext.append(childNode.getNodeValue().trim());
        }
    }

    return childtext.toString();
}

From source file:Main.java

/**
 * Get a list of all child text Elements of given name directly
 * under a given {@code org.w3c.dom.Element}.
 *
 * @param elem the parent Element//  ww w.  j a  v  a2 s  .  c  om
 * @param name the given name of searched child Elements
 * @return a List of values of those child text Elements
 */
public static List<String> getAllElementsByTagName(Element elem, String name) {
    NodeList nodeList = elem.getElementsByTagName(name);
    List<String> result = new ArrayList<String>();
    for (int i = 0; i < nodeList.getLength(); ++i) {
        NodeList children = nodeList.item(i).getChildNodes();
        if (children.getLength() == 0 || children.item(0).getNodeType() != Node.TEXT_NODE) {
            continue;
        }
        result.add(children.item(0).getNodeValue());
    }
    return result;
}

From source file:Main.java

static public String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE) {
                    return data.getNodeValue();
                }/*from  www.jav  a  2  s  . c  o m*/
            }
        }
    }
    return "";
}

From source file:Utils.java

/**
 * Get the specified text node associated with this element
 * @param parent the node containing text
 * @param number The text node to fetch (1st, 2nd, etc)
 * @return Text (trimmed of leanding/trailing whitespace, null if none)
 *//*from   w  ww .  ja  va2s  .c  o m*/
public static String getTextNodeByNumber(Node parent, int number) {
    String text = null;
    int count = 1;

    if (parent != null) {
        for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {

            if ((child.getNodeType() == Node.TEXT_NODE) && (count++ == number)) {
                text = child.getNodeValue();
                return text.trim();
            }
        }
    }
    return text;
}