Example usage for org.w3c.dom Node getNodeValue

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

Introduction

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

Prototype

public String getNodeValue() throws DOMException;

Source Link

Document

The value of this node, depending on its type; see the table above.

Usage

From source file:Main.java

/**
 * Searches parent node for matching child nodes, then collects and returns
 * the first match node's value. Useful when caller knows that there is only
 * one child node./*w  w w  . j  ava2 s.  com*/
 *
 * @param node     The parent node
 * @param nodeName The child node element name
 * @return The child node's value
 */
public static String getNodeValue(Node node, String nodeName) {
    Node n = getNode(node, nodeName);
    if (n == null) {
        return null;
    }
    Node ch = n.getFirstChild();
    if (ch != null) {
        return ch.getNodeValue();
    }
    return null;

}

From source file:Main.java

private static void printNote(NodeList nodeList, int depth) {
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
            System.out.println(depth + "Node Value =" + tempNode.getTextContent());
            if (tempNode.hasAttributes()) {
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                }//from  w  w  w.  j av a 2s  . c  om
            }
            if (tempNode.hasChildNodes()) {
                printNote(tempNode.getChildNodes(), depth + 1);
            }
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
        }
    }
}

From source file:Utils.java

/**
 * Get the raw text content of a node or null if there is no text
 *//* w w  w . j  a  v a  2  s  .c o m*/
public static String getRawContent(Node n) {
    if (n == null) {
        return null;
    }

    Node n1 = getChild(n, Node.TEXT_NODE);

    if (n1 == null) {
        return null;
    }

    return n1.getNodeValue();
}

From source file:Main.java

public static String nodeToString(Node n) {
    StringBuilder sb = new StringBuilder();
    if (n instanceof Text)
        return n.getNodeValue().trim();
    else {// ww w  .  ja va  2s .  c o m
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node child = nl.item(i);
            sb.append(nodeToString(child));
        }
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if ("td".equalsIgnoreCase(n.getNodeName())) {
                sb.append("\t");
            } else if ("th".equalsIgnoreCase(n.getNodeName())) {
                sb.append("\t");
            } else if ("tr".equalsIgnoreCase(n.getNodeName())) {
                sb.append("\n");
            } else if ("br".equalsIgnoreCase(n.getNodeName())) {
                sb.append("\n");
            }
        }
        return sb.toString();
    }
}

From source file:Main.java

/**
 * Get the attribute value from specified node.
 * //from  ww  w  .  j ava  2 s . c om
 * @param node
 *          the node
 * @param attributeName
 *          the attribute name
 * @return the attribut value
 */
public static String getNodeAttribute(Node node, String attributeName) {
    if (null == node) {
        return null;
    }
    NamedNodeMap attributes = node.getAttributes();
    if (null == attributes) {
        return null;
    }
    Node n = attributes.getNamedItem(attributeName);
    if (null == n) {
        return null;
    }
    return n.getNodeValue();
}

From source file:Main.java

private static void extractText(Node n, StringBuffer buf) {
    if (n.getNodeType() == Node.TEXT_NODE) {
        buf.append(n.getNodeValue());
    }//w ww. j ava2 s  .c o  m

    for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) {
        extractText(n, buf);
    }
}

From source file:Main.java

public static String getNodeAttr(String attrName, Node node) {
    NamedNodeMap attrs = node.getAttributes();
    for (int y = 0; y < attrs.getLength(); y++) {
        Node attr = attrs.item(y);
        if (attr.getNodeName().equalsIgnoreCase(attrName)) {
            return attr.getNodeValue();
        }/*www.  j  a  va  2s.c  o  m*/
    }
    return "";
}

From source file:Main.java

public static String getTextContent(Node e) {
    if (e == null || e.getNodeType() != Node.ELEMENT_NODE) {
        return null;
    }//w w  w .  ja v a  2 s.  co m

    NodeList nodes = e.getChildNodes();
    StringBuilder text = new StringBuilder();

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

        if (node != null && node.getNodeType() == Node.TEXT_NODE) {
            String s = node.getNodeValue();
            if (s != null) {
                text.append(s);
            }
        }
    }

    if (text.length() > 0) {
        return text.toString();
    } else {
        return null;
    }
}

From source file:Main.java

public static Node findNodeByAttribute(Document document, String tagName, String attributeName,
        String attributeValue) {//  www  . j  a  va  2 s .co  m
    Node foundNode = null;
    NodeList nodes = document.getElementsByTagName(tagName);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        for (int j = 0; j < node.getAttributes().getLength(); j++) {
            Node attribute = node.getAttributes().item(j);
            if (attribute.getNodeName().equals(attributeName)
                    && attribute.getNodeValue().equals(attributeValue)) {
                foundNode = node;
                break;
            }
        }
        if (foundNode != null)
            break;
    }
    return foundNode;
}

From source file:Utils.java

/**
 * Get all prefixes defined on this element for the specified namespace.
 * //from  w  w  w.  ja v a2 s .  com
 * @param element
 * @param namespaceUri
 * @param prefixes
 */
public static void getPrefixes(Element element, String namespaceUri, List<String> prefixes) {
    NamedNodeMap atts = element.getAttributes();
    for (int i = 0; i < atts.getLength(); i++) {
        Node node = atts.item(i);
        String name = node.getNodeName();
        if (namespaceUri.equals(node.getNodeValue())
                && (name != null && (XMLNAMESPACE.equals(name) || name.startsWith(XMLNAMESPACE + ":")))) {
            prefixes.add(node.getPrefix());
        }
    }
}