0

I have made a simple demo weather app in android for the learning purpose,I have 1 api(yahoo weather api) for data to be parsed out from it,can any buddy tell me how to get "code" value for the "yweather;forecast" xml tag for every day? My api lik is yahoo weather api My xml is as below: weather:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
        <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
            <channel>

<title>Yahoo! Weather - Dubai, AE</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Dubai__AE/*http://weather.yahoo.com/forecast/AEXX0004_f.html</link>
<description>Yahoo! Weather for Dubai, AE</description>
<language>en-us</language>
<lastBuildDate>Fri, 03 Jan 2014 7:58 am GST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Dubai" region=""   country="United Arab Emirates"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="63"   direction="180"   speed="3" />
<yweather:atmosphere humidity="77"  visibility="6.21"  pressure="30.18"  rising="1" />
<yweather:astronomy sunrise="7:03 am"   sunset="5:38 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
</image>
<item>
<title>Conditions for Dubai, AE at 7:58 am GST</title>
<geo:lat>25.27</geo:lat>
<geo:long>55.31</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Dubai__AE/*http://weather.yahoo.com/forecast/AEXX0004_f.html</link>
<pubDate>Fri, 03 Jan 2014 7:58 am GST</pubDate>
<yweather:condition  text="Fair"  code="34"  temp="63"  date="Fri, 03 Jan 2014 7:58 am GST" />
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/34.gif"/><br />
<b>Current Conditions:</b><br />
Fair, 63 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Sunny. High: 73 Low: 61<br />
Sat - Sunny. High: 73 Low: 58<br />
Sun - Sunny. High: 75 Low: 62<br />
Mon - AM Clouds/PM Sun. High: 76 Low: 62<br />
Tue - Scattered Showers. High: 69 Low: 59<br />
Wed - Partly Cloudy. High: 73 Low: 58<br />
Thu - Partly Cloudy. High: 72 Low: 58<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Dubai__AE/*http://weather.yahoo.com/forecast/AEXX0004_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
<yweather:forecast day="Fri" date="3 Jan 2014" low="61" high="73" text="Sunny" code="32" />
<yweather:forecast day="Sat" date="4 Jan 2014" low="58" high="73" text="Sunny" code="32" />
<yweather:forecast day="Sun" date="5 Jan 2014" low="62" high="75" text="Sunny" code="32" />
<yweather:forecast day="Mon" date="6 Jan 2014" low="62" high="76" text="AM Clouds/PM Sun" code="30" />
<yweather:forecast day="Tue" date="7 Jan 2014" low="59" high="69" text="Scattered Showers" code="39" />
<yweather:forecast day="Wed" date="8 Jan 2014" low="58" high="73" text="Partly Cloudy" code="30" />
<yweather:forecast day="Thu" date="9 Jan 2014" low="58" high="72" text="Partly Cloudy" code="30" />
<guid isPermaLink="false">AEXX0004_2014_01_09_7_00_GST</guid>
</item>
</channel>
</rss>

<!-- fan474.sports.gq1.yahoo.com Fri Jan  3 04:36:49 PST 2014 -->

please help me for it

4 Answers 4

0

You can refer the following link for xml parsing (Android recommends XmlPullParser): http://developer.android.com/training/basics/network-ops/xml.html

Sign up to request clarification or add additional context in comments.

Comments

0
XmlPullParser xpp = getResources().getXml(R.xml.answer);
    try {
        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            switch (xpp.getEventType()) {
            case XmlPullParser.START_TAG:
                if (xpp.getName().equals("forecast")) {
                    for (int i = 0; i < xpp.getAttributeCount(); i++) {
                        if (xpp.getAttributeName(i).equals("code"))
                            Log.d("myLogs", "code=" + xpp.getAttributeValue(i));
                    }
                    break;
                }
            default:
                break;
            }
            xpp.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

XmlPullParser method getName() ignoring xml namespace, so if you try to execute xpp.getName() on xml like

<yweather:forecast day="Fri" date="3 Jan 2014" low="61" high="73" text="Sunny" code="32" />

it will return just "forecast".

3 Comments

what is "r.xml"? how to create and which value i should add
Create folder "xml" in your res directory and add your xml file. So structure looks like this: res/xml/answer.xml And answer.xml is your xml file.
oh! sorry. check this link to find how to pass xml from web using xmlPullParser. stackoverflow.com/questions/5409940/…
0

you can copy this code immediatly.

    private class Weather{
    public String day;
    public String date;
    public String low;
    public String high;
    public String text;
    public String code;
}
private void xmlParsingDemo() throws Exception{
    // init
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser parser = factory.newPullParser();
    String path = getFilesDir() + File.separator + "weather.xml";
    parser.setInput(new FileInputStream(path), "utf-8");
    // start to parse
    List<Weather> weathers = new ArrayList<Weather>();
    Weather weather = null;
    int eventType = parser.getEventType();
    while(eventType != XmlPullParser.END_DOCUMENT){
        if(eventType == XmlPullParser.START_TAG && "forecast".equals(parser.getName())){
            weather = new Weather();
            weather.day = parser.getAttributeValue(null, "day");
            weather.date = parser.getAttributeValue(null, "date");
            weather.low = parser.getAttributeValue(null, "low");
            weather.high = parser.getAttributeValue(null, "high");
            weather.text = parser.getAttributeValue(null, "text");
            weather.code = parser.getAttributeValue(null, "code");
            weathers.add(weather);
        }
        eventType = parser.next();
    }

    //by now
    //the list weathers is all of the weather you want
}

Comments

-1

Hope these Link help you

1- https://www.google.co.in/?gws_rd=cr&ei=SWbGUuSSMorkrAePmYDIBA#q=how+to+get+xml+attribute+in+android+sax+parsing

2- Parse Xml to get attribute value in android using android SAX packages mathods

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.