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

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuild = dbf.newDocumentBuilder();
    Document doc = docBuild.parse(new File("test2.xml"));

    Element x = doc.getDocumentElement();
    NodeList m = x.getChildNodes();
    for (int i = 0; i < m.getLength(); i++) {
        Node it = m.item(i);
        if (it.getNodeType() == 3) {
            System.out.println(it.getNodeValue());
        }/* ww  w.  j a va2  s  .c  om*/
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<String> names = new ArrayList<>();
    URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265");
    InputStream is = oracle.openStream();
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);//from  w w w  .j  a  va  2  s. co  m
    DocumentBuilder builder = domFactory.newDocumentBuilder();
    Document doc = builder.parse(is);
    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();
    XPathExpression expr = xpath.compile("//*:*/@*");
    Object result = expr.evaluate(doc, XPathConstants.NODESET);
    NodeList nl = (NodeList) result;
    for (int i = 0; i < nl.getLength(); i++) {
        names.add(nl.item(i).getNodeName());
        Node node = nl.item(i);
        String path = "." + node.getNodeName() + " = " + node.getNodeValue();
        node = ((Attr) node).getOwnerElement();
        while (node != null) {
            path = node.getNodeName() + '/' + path;
            node = node.getParentNode();
        }
        System.out.println(path);
    }
}

From source file:Main.java

public static void main(String args[]) throws IOException, SAXException {

    DOMParser parser = new DOMParser();
    parser.parse("games.xml");

    Document dom = parser.getDocument();

    NodeList games = dom.getElementsByTagName("game");

    for (int i = 0; i < games.getLength(); i++) {
        Node aNode = games.item(i);
        System.out.println(aNode.getFirstChild().getNodeValue());

        NamedNodeMap attributes = aNode.getAttributes();

        for (int a = 0; a < attributes.getLength(); a++) {
            Node theAttribute = attributes.item(a);
            System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
        }//from   ww w  .j  a v  a2  s .c o m
    }

}

From source file:DemoTreeWalker.java

public static void main(String args[]) throws SAXException, IOException {

    DOMParser parser = new DOMParser();
    parser.parse("games.xml");

    Document doc = parser.getDocument();

    DocumentTraversal docTraversal = (DocumentTraversal) doc;

    TreeWalker iter = docTraversal.createTreeWalker(doc.getDocumentElement(), NodeFilter.SHOW_ALL, null, false);
    Node n = null;
    while ((n = iter.nextNode()) != null) {
        System.out.println("Node name: " + n.getNodeName());
        System.out.println("Node value: " + n.getNodeValue());
    }//  ww w  .j a v a 2  s  . c  o m

}

From source file:Main.java

public static String getElementValue(Node node) {

    return node.getNodeValue();
}

From source file:Main.java

static String getValue(String tag, Element element) {
    NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
    Node node = (Node) nodes.item(0);
    return node.getNodeValue();
}

From source file:Main.java

private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0);
    return nValue.getNodeValue();
}

From source file:Main.java

public static String getNodeValue(Node node) {
    return node.getNodeValue();
}

From source file:Main.java

public static void printNode(Node node) {
    System.out.println(node.getNodeValue());
}

From source file:Main.java

/**
 * Gets a node value./*from w w  w  .j  av  a 2s .c o m*/
 *
 * @param pNode
 *            the p node
 * @return the node value
 */
public static String getNodeValue(Node pNode) {
    if (pNode.getNodeValue() != null) {
        return pNode.getNodeValue();
    } else if (pNode.getFirstChild() != null) {
        return getNodeValue(pNode.getFirstChild());
    } else {
        return null;
    }
}