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 orEmptyStr(XPathExpression xpe, Node n) throws XPathExpressionException {
    Node inner = (Node) xpe.evaluate(n, XPathConstants.NODE);
    return inner == null ? "" : inner.getTextContent();
}

From source file:Main.java

/**
 * Given an XML string, return the textContent of all of the tags that match tagName as a list.
 * @param xmlStr/*www.j a  v a  2 s  .co m*/
 * @param tagName
 * @method getValuesForNode
 * @static
 */
public static List<String> getValuesForNode(String xmlStr, String tagName) {

    List<String> values = new ArrayList<String>();
    try {

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));

        Document doc = docBuilder.parse(is);

        doc.getDocumentElement().normalize();

        NodeList nodes = doc.getElementsByTagName(tagName);
        int nodeCount = nodes.getLength();

        for (int i = 0; i < nodeCount; i++) {
            Node node = nodes.item(i);
            values.add(node.getTextContent());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return values;

}

From source file:Main.java

public static String findElementText(Node node, String childTag) {
    if (node == null)
        return null;
    Node n = findNode(node, childTag);
    if (n != null)
        return n.getTextContent().trim();
    return null;/*from  www.j a va2s  . c om*/
}

From source file:Main.java

public static String getChildElementText(Node node, String childTag) {
    if (node == null)
        return null;
    Node n = getChildNode(node, childTag);
    if (n != null)
        return n.getTextContent().trim();
    return null;/*www  .  j  a  v a2 s. co  m*/
}

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());
    }//  ww  w. j  a va2  s  .  com
    return textContent.toString().trim();
}

From source file:Main.java

public static String getElementText(final Element eElement, final String name) {
    NodeList nodes = eElement.getElementsByTagName(name);
    if (nodes == null) {
        return null;
    }//ww  w  . j  a  va 2 s . co m
    Node node = nodes.item(0);
    if (node == null) {
        return null;
    }
    String value = node.getTextContent();
    return (value != null) ? value.trim() : null;
}

From source file:Main.java

public static String readAttribute(Node element, String attributeName) {
    if (element == null)
        return null;
    NamedNodeMap attributes = element.getAttributes();
    if (attributes == null)
        return null;
    Node value = attributes.getNamedItem(attributeName);
    if (value == null)
        return null;
    return value.getTextContent();
}

From source file:Main.java

/**
 * Parses the value of the given attribute as an float.
 * @param attributeName the name of the attribute.
 * @param map the map that contains all the attributes.
 * @return the float value.// ww w. j av a2 s  .c  o  m
 */
public static int parseInt(String attributeName, NamedNodeMap map) {
    org.w3c.dom.Node attr = map.getNamedItem(attributeName);
    try {
        return attr != null ? Integer.parseInt(attr.getTextContent()) : 0;
    } catch (NumberFormatException ex) {
        return 0;
    }
}

From source file:Main.java

/**
 * Parses the value of the given attribute as a float.
 * @param attributeName the name of the attribute.
 * @param map the map that contains all the attributes.
 * @return the float value.//  w w  w .  j a v a2 s.  c om
 */
public static float parseFloat(String attributeName, NamedNodeMap map) {
    org.w3c.dom.Node attr = map.getNamedItem(attributeName);
    try {
        return attr != null ? Float.parseFloat(attr.getTextContent()) : 0f;
    } catch (NumberFormatException ex) {
        return 0.0f;
    }
}

From source file:Main.java

public static String getTextofChildNode(Node parentNode, String nameOfChildToFind) {
    Node nodeToFind = getChildNodebyName(parentNode, nameOfChildToFind);
    if (nodeToFind != null)
        return nodeToFind.getTextContent();
    else/*  ww  w  .  ja v a  2 s .  co m*/
        return "";
}