I am trying to parse the XML file below :
<current> <city id="1273840" name="Connaught Place"> <coord lon="77.22" lat="28.63"/> <country>IN</country> <sun rise="2016-04-11T00:29:24" set="2016-04-11T13:15:01"/> </city> <temperature value="308.15" min="308.15" max="308.15" unit="kelvin"/> <humidity value="17" unit="%"/> <pressure value="1010" unit="hPa"/> I am successfully able to get the value of country tag("IN") but not able to get the value of attribute "value" or any value of attribute for temperature,pressure or any other tags mentioned above.I am getting null as the value. Following is the code I have written:
protected String doInBackground(Void... params) { try { URL u = new URL(URL); HttpURLConnection con = (HttpURLConnection) u.openConnection(); con.setReadTimeout(10000); con.setConnectTimeout(10000); con.setRequestMethod("GET"); con.setDoInput(true); con.connect(); InputStream i = con.getInputStream(); xf = XmlPullParserFactory.newInstance(); xp = xf.newPullParser(); xp.setInput(i, null); xp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,false); event = xp.getEventType(); } catch(Exception e) { e.printStackTrace(); } while(event!=XmlPullParser.END_DOCUMENT){ name=xp.getName(); switch(event){ case XmlPullParser.START_TAG: break; case XmlPullParser.TEXT: text=xp.getText(); break; case XmlPullParser.END_TAG: if(name.equals("country")){ country=text; break; } else if(name.equals("temperature")){ temperature=xp.getAttributeValue(null,"value"); publishProgress(temperature); break; } else break; } try { event=xp.next(); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } Can someone please help me understand the issue here..
No comments:
Post a Comment