I have an xml: http://netmobileag.accu-weather.com/widget/netmobileag/weather-data.asp?slat=48.04960&slon=21.71420&metric=1
I just want to get city and temperature, i tried this:
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGethttp://netmobileag.accu weather.com/widget/netmobileag/weather-data.asp?slat=48.04960&slon=21.71420&metric=1");
HttpResponse response = httpclient.execute(httpget);
InputStream in = response.getEntity().getContent();
String res = "";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(in));
doc.getDocumentElement().normalize();
NodeList loc = doc.getElementsByTagName("local");
for (int i = 0; i < loc.item(0).getChildNodes().getLength(); i++) {
res = res + "\n" + loc.item(0).getChildNodes().item(0).getNodeValue();
}
But the res will be
#text
#text
...
summary 25x. How can I get the city and temperature value?
The right thing to do here is to use Xpath actually. Do you search in a Database with Regex or SQL? Same here, searching in XML files is done via XPath.
}
Try the below code
try getTextContent() instead of getNodeValue()