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(String attributeName, Node xmlNode) {
    if (xmlNode == null)
        return null;

    if (xmlNode.getAttributes() == null)
        return null;

    if (xmlNode.getAttributes().getLength() == 0)
        return null;

    Node n = xmlNode.getAttributes().getNamedItem(attributeName);

    if (n == null)
        return null;

    return n.getTextContent();
}

From source file:Main.java

public static String getAttribute(Node node, String attributeName) {
    Node attribute = node.getAttributes().getNamedItem(attributeName);
    if (attribute == null) {
        return null;
    }// w  w w.j a v  a2  s.  c  o m

    return attribute.getTextContent();
}

From source file:Main.java

public static String getValueXPath(String srcXmlString, String xPath) {
    String value = null;/*from w  ww . j a  v  a2  s.  c o m*/
    try {
        Object result = execXpathGetNode(srcXmlString, xPath);
        Node node = (Node) result;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            value = node.getTextContent();
        } else {
            value = node.getNodeValue();
        }
        logger.debug(xPath + " = " + value);
    } catch (Exception ex) {
        logger.error(ex.getMessage() + " Could not extract any value using xpath: " + xPath);
    }
    return value;
}

From source file:Main.java

public static String getTextContent(Node node) {

    if (node == null)
        throw new NullPointerException();

    String textContent;/*from w ww . j a  v a 2 s  . com*/

    textContent = node.getTextContent();
    if (textContent != null)
        return xmlDecode(textContent);

    NodeList childNodes = node.getChildNodes();
    if (childNodes.getLength() > 0 && childNodes.item(0) instanceof Text)
        textContent = node.getNodeValue();
    if (textContent != null)
        return xmlDecode(textContent);

    return "";
}

From source file:Main.java

/**
 *//*from   w  w w .j  a v a 2s.  c  om*/
public static String getComment(Element searchIn, int commentIndex) {
    NodeList list = searchIn.getChildNodes();
    int counter = 0;
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (n.getNodeType() == Node.COMMENT_NODE) {
            if (counter == commentIndex) {
                return n.getTextContent();
            }
            counter++;
        }
    }
    return null;
}

From source file:Main.java

public static List<String> evaluateNodeListTextXPath(final Document document, final XPathExpression expression)
        throws XPathExpressionException {
    notNull(document);/*from  www  .  j  a  v  a 2 s.  c om*/
    notNull(expression);

    final List<String> result = new LinkedList<String>();

    for (final Node node : evaluateNodeListXPath(document, expression)) {
        result.add(node.getTextContent());
    }

    return result;
}

From source file:Main.java

/**
 * Getter for a specific text content of the given node.
 * @param node Node.//www . ja v  a 2  s  . com
 * @param name Name of element tags around the text content.
 * @return Returns the content or null if no content with the given
 * name exists.
 */
static public String getTextContent(Node node, String name) {
    node = node.getFirstChild();
    while (node != null) {
        if (node.getNodeName().equals(name))
            return (node.getTextContent());
        node = node.getNextSibling();
    }
    return (null);
}

From source file:Main.java

/**
 * Finds the DOM node with specified name from a node list and returns
 * its content as a string./* w  ww  .  ja  v  a2s  .c  om*/
 * @param nodes Node list
 * @param name Node name
 * @return Content of the node as a string, or null if no node/content
 * @example
 * <pre name="test">
 * Document d = createDomDocument();
 * Node root = d.appendChild( d.createElement("Root") );
 * Node n1 = root.appendChild( d.createElement("Node1") );
 * n1.appendChild( d.createTextNode("Value1") );
 * Element e2 = d.createElement("Node2");
 * e2.appendChild( d.createTextNode("Value2") );
 * Node n2 = root.appendChild( e2 );
 * getNodeContent(root.getChildNodes(), "Node1") === "Value1";
 * getNodeContent(root.getChildNodes(), "Node2") === "Value2";
 * getNodeContent(root.getChildNodes(), "NotFound") === null;
 * </pre>
 */
public static String getNodeContent(NodeList nodes, String name) {
    Node node = getNodeByName(nodes, name);
    if (node == null || node.getFirstChild() == null)
        return null;
    return node.getTextContent();
}

From source file:Main.java

public static Map<String, String> getChildElementNodesMap(Node node) {
    if (node == null) {
        return null;
    }/*from   www.j  av  a  2  s .com*/

    Map<String, String> map = new ConcurrentHashMap<>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null && item.getNodeType() == Node.ELEMENT_NODE) {
                map.put(item.getNodeName(), item.getTextContent().trim());
            }
        }
    }
    return map;
}

From source file:Main.java

public static Map<String, String> XmlAsMap(Node node) {
    Map<String, String> map = new HashMap<String, String>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
        if (currentNode.hasAttributes()) {
            for (int j = 0; j < currentNode.getAttributes().getLength(); j++) {
                Node item = currentNode.getAttributes().item(i);
                if (item != null)
                    map.put(item.getNodeName(), prepare(item.getTextContent()));
            }//from   www  .  jav a  2s . co  m
        }
        if (currentNode.getFirstChild() != null) {
            if (currentNode.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
                map.putAll(XmlAsMap(currentNode));
            } else if (currentNode.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                map.put(currentNode.getLocalName(), prepare(currentNode.getTextContent()));
            }
        }
    }
    return map;
}