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 getAttribute(Node node, String name) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes == null) {
        return null;
    }/*from  www  . j ava  2 s. c  o  m*/

    Node attributeNode = node.getAttributes().getNamedItem(name);
    if (attributeNode == null) {
        return null;
    }
    return attributeNode.getTextContent();
}

From source file:Main.java

public static String getNodeTextValue(File xmlfile, String nodeXpath)
        throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
    //logger.debug("Getting the Text value of the node");

    // get the Document object.
    Document doc = getDocument(xmlfile);

    // get the Node Object for the xpath.
    Node node = getNodeObject(nodeXpath, doc);

    // Get the Attribute Value returned as a String
    String NodeTextValue = node.getTextContent();
    //logger.debug("Node name " + nodeXpath + " and the text contains is " + NodeTextValue);
    //logger.debug("Returning the node text value as " + NodeTextValue);

    return NodeTextValue;
}

From source file:Main.java

public static String GetChildElementText(Element element, String name) {
    if (element != null && name != null && !name.isEmpty()) {
        NodeList nodes = element.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node instanceof Element && name.equals(node.getNodeName())) {
                return node.getTextContent();
            }// w  w w. j  av  a 2s  .  c  o  m
        }
    }
    return null;
}

From source file:Main.java

public static String GetChildElementText(Element element, String name, String defaultValue) {
    if (element != null && name != null && !name.isEmpty()) {
        NodeList nodes = element.getChildNodes();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node instanceof Element && name.equals(node.getNodeName())) {
                return node.getTextContent();
            }//from   w w  w .ja va  2  s . co  m
        }
    }
    return defaultValue;
}

From source file:Main.java

public static Object transformXmlNodesIntoMap(Node node) {
    Map<String, Object> nodeMap = new HashMap<String, Object>();

    NodeList subNodes = node.getChildNodes();

    NamedNodeMap nodeAttrs = node.getAttributes();
    for (int nodeAttrIdx = 0; nodeAttrIdx < nodeAttrs.getLength(); nodeAttrIdx++) {
        Node attrNode = nodeAttrs.item(nodeAttrIdx);
        nodeMap.put("@" + attrNode.getNodeName(), attrNode.getTextContent());
    }/*ww  w .  j a v  a 2 s . c o  m*/

    if (nodeAttrs.getLength() == 0)
        if (subNodes.getLength() == 0)
            return "";
        else if (subNodes.getLength() == 1 && subNodes.item(0).getNodeType() == Node.TEXT_NODE)
            return subNodes.item(0).getTextContent();

    for (int subNodeIdx = 0; subNodeIdx < subNodes.getLength(); subNodeIdx++) {
        Node subNode = subNodes.item(subNodeIdx);

        if (subNode.getNodeType() == Node.TEXT_NODE) {
            nodeMap.put(subNode.getNodeName(), subNode.getTextContent());
        } else {
            if (nodeMap.containsKey(subNode.getNodeName())) {
                Object subObject = nodeMap.get(subNode.getNodeName());
                if (subObject instanceof List<?>) {
                    ((List<Object>) subObject).add(transformXmlNodesIntoMap(subNode));
                } else {
                    List<Object> subObjectList = new ArrayList<Object>();
                    subObjectList.add(subObject);
                    subObjectList.add(transformXmlNodesIntoMap(subNode));
                    nodeMap.put(subNode.getNodeName(), subObjectList);
                }
            } else {
                nodeMap.put(subNode.getNodeName(), transformXmlNodesIntoMap(subNode));
            }
        }

    }
    return nodeMap;
}

From source file:Main.java

/**
 * Prints out the DOM tree./*from ww w  . j  ava2  s . com*/
 * 
 * @param n
 *            the parent node to start printing from
 * @param prefix
 *            string to append to output, should not be null
 */
public static void printDom(Node n, String prefix, StringBuilder sb) {
    String outString = prefix;
    if (n.getNodeType() == Node.TEXT_NODE) {
        outString += "'" + n.getTextContent().trim() + "'";
    } else {
        outString += n.getNodeName();
    }
    sb.append(outString);
    sb.append("\n");
    NodeList children = n.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        printDom(children.item(i), prefix + "  ", sb);
    }
}

From source file:Main.java

public static boolean isLeaf(Node node) {

    NodeList nodeList = node.getChildNodes();

    boolean hasElement = false;

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node child = nodeList.item(i);
        if (child instanceof Text) {
            if (!"".equals(child.getTextContent().trim())) {
                // The current node is a leaf node if it contains
                // at least one text node with non-empty text values
                return true;
            }//from w  w  w.java2s . c o m
        }

        if (child instanceof Element) {
            hasElement = true;
        }
    }

    // The current node is also a leaf node if it
    // contains no elements at all
    return !hasElement;
}

From source file:Main.java

/**
 * Returns the text content of a node or an empty string if an error occurs.
 * //from   www  .j a  v  a2s . co  m
 * @param node The node.
 * @param defaultValue The default value.
 * @return The text content or the default value.
 */
public static String getTextContent(Node node, String defaultValue) {
    try {
        String content = null;
        if (node != null) {
            content = node.getTextContent();
        }
        return content != null ? content : defaultValue;
    } catch (Exception e) {
        return defaultValue;
    }
}

From source file:Main.java

public static void collectXpathContainText(Node node, String textContent, List<String> holder) {
    if (textContent.equals(node.getTextContent())) {
        String xpath = getXPath(node);
        if (!holder.contains(xpath)) {
            holder.add(xpath);/*  w  w  w  . j  a v  a 2 s . c  o m*/
        }
    }

    if (node.hasChildNodes()) {
        Node child = node.getFirstChild();
        while (child != null) {
            collectXpathContainText(child, textContent, holder);
            child = child.getNextSibling();
        }
    }
}

From source file:Main.java

public static String getAttribute(Node node, String localName, String namespaceURI) {
    Node attributeNode = node.getAttributes().getNamedItemNS(namespaceURI, localName);
    if (attributeNode == null) {
        return null;
    }/*w  ww.  j a  v  a  2s .c o m*/
    return attributeNode.getTextContent();
}