I have tried a lot of times but I did not how to retrive a value from XML using Java. I tried to use DOM and Xpath. Please help. I can use a String Writer to printout the XML so I know the XML is not empty.
Document doc = parseXML(connection.getInputStream());
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xPath.compile("/xml_api_reply/weather/current_conditions/temp_f/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
The content of XML :
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
<current_conditions>
<condition data="Clear"/>
<temp_f data="49"/>
<temp_c data="9"/>
</current_conditions>
</weather>
</xml_api_reply>
It seems that it did not go in to the for loop because nodes is null.