Example usage for org.w3c.dom Node getTextContent

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

Introduction

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

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:Main.java

public static String getAttributeValue(Node node, String attribute) {
    Node att = node.getAttributes().getNamedItem(attribute);

    if (att == null)
        return null;

    return att.getTextContent();
}

From source file:Main.java

/**
 * Gets the next comment.//from  w  w w. jav  a 2  s  . co m
 * 
 * @param element
 *            the element
 * @return the next comment
 */
public static String getNextComment(Node element) {
    while (element.getNextSibling() != null) {
        Node prev = element.getNextSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getNextComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets a node's text value given its parent node and the name of the wanted child node.<br>
 * //  w  w  w  . j a  v a  2s . com
 * @param fatherNode the parent node
 * @param nodeName name of the wanted child node
 * @return String text node value
 */
public static String getNodeValue(Node fatherNode, String nodeName) {
    String value = null;
    if (((Element) fatherNode).getElementsByTagName(nodeName) != null) {
        Node node = ((Element) fatherNode).getElementsByTagName(nodeName).item(0);
        value = node.getTextContent();
        if (value != null) {
            value = value.trim();
        }
    }
    return value;
}

From source file:Main.java

public static String getAttribute(Node node, String attributeName, String defaultValue) {
    Node attribute = node.getAttributes().getNamedItem(attributeName);
    return attribute != null ? attribute.getTextContent().trim() : defaultValue;
}

From source file:Main.java

/**
 * Gets the previous comment./*w  ww .ja v  a2s .c  o m*/
 * 
 * @param element
 *            the element
 * @return the previous comment
 */
public static String getPreviousComment(Node element) {
    while (element.getPreviousSibling() != null) {
        Node prev = element.getPreviousSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getPreviousComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Hack...since DOM reads newlines as textnodes we want to strip out those nodes to make it easier to use the tree.
 *//*from w w w.ja  va2s  .  c  o  m*/
public static void stripEmptyTextNodes(Node n) {
    NodeList children = n.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        Node c = children.item(i);
        if (!c.hasChildNodes() && c.getNodeType() == Node.TEXT_NODE
                && c.getTextContent().trim().length() == 0) {
            n.removeChild(c);
            i--;
            length--;
            children = n.getChildNodes();
        } else {
            stripEmptyTextNodes(c);
        }
    }
}

From source file:Main.java

public static String[] getNodesValue(Element node, String nodeName) {
    List<String> ret = new ArrayList<String>();

    NodeList ndLs = node.getElementsByTagName(nodeName);
    for (int s = 0; s < ndLs.getLength(); s++) {
        Node fstNode = ndLs.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
            ret.add(fstNode.getTextContent());
        }// w  w w. j  a  v  a2 s  .  c  om
    }

    return ret.toArray(new String[0]);
}

From source file:Main.java

private static String getNodeName(Node node) {
    String name = "";
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node curNode = nodeList.item(i);
        if ("name".equalsIgnoreCase(curNode.getNodeName())) {
            name = curNode.getTextContent().replace("\"", "");
            break;
        }/*from   w  ww  .ja v  a 2  s  .c o m*/
    }

    return name;
}

From source file:Main.java

public static String getChildNodeTextContents(Node node, String name) {
    Node tempNode = getChildNode(node, name);
    if (tempNode != null)
        return (tempNode.getTextContent());
    else//from ww w .j a va  2 s .c  o  m
        return (null);
}

From source file:Main.java

/**
 * A safe way of getting attribute value of attribute with given name. If attribute with given name
 * doesn't exist, returns null// w ww.  j  a  va2 s  .c o m
 * (instead of NPE).
 * 
 * @param node
 * @param attributeName
 * @return
 */
public static String getAttribute(Node node, String attributeName) {
    if (node == null)
        return null;

    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null)
        return null;

    Node attribute = attributes.getNamedItem(attributeName);
    if (attribute == null)
        return null;

    return attribute.getTextContent();
}