RSS-Datei auslesen von Yahoo Webservice

mavinatic

Bekanntes Mitglied
Hallo Community,

mein Problem ist folgendes ich möchte Yahoo's Wetter Webservice benutzten, soweit so gut. Ich habe mit dem InputStream die RSS/XML-Datei erhalten und mag nun Werte auslesen 🙂 aber komischerweise erhalte ich immer eine NullpointerException?! Ich habe alles durchdebugged und Sysout geprüft alles regulär...ich finds sehr komisch 😀 könntet ihr mal'n blick drüber werfen?

Java:
public void getRequestData(Element root) {
		
		Element dataElement = root.getChild("channel");
		/* general info */
		title = dataElement.getChild("title").getValue();
		description = dataElement.getChild("description").getValue();
		
		/* location info */
		locationCity = dataElement.getChild("yweather:location").getAttribute("city").getValue();
		System.out.println(locationCity);
		locationRegion = dataElement.getChild("yweather:location").getAttribute("region").getValue();
		locationCountry = dataElement.getChild("yweather:location").getAttribute("country").getValue();
		
		/* unit info */
		unitTemperature = dataElement.getChild("yweather:units").getAttribute("temperature").getValue();
		unitDistance = dataElement.getChild("yweather:units").getAttribute("distance").getValue();
		unitPressure = dataElement.getChild("yweather:units").getAttribute("pressure").getValue();
		unitSpeed = dataElement.getChild("yweather:units").getAttribute("speed").getValue();
		
		/* wind info */
		windChill = dataElement.getChild("yweather:wind").getAttribute("chill").getValue();
		windDirection = dataElement.getChild("yweather:wind").getAttribute("direction").getValue();
		windSpeed = dataElement.getChild("yweather:wind").getAttribute("speed").getValue();
		
		/* atmosphere info */
		atmosphereHumidity = dataElement.getChild("yweather:atmosphere").getAttribute("humidity").getValue();//feuchtigkeit
		atmosphereVisibility = dataElement.getChild("yweather:atmosphere").getAttribute("visibility").getValue();
		atmospherePressure = dataElement.getChild("yweather:atmosphere").getAttribute("pressure").getValue(); 
		atmosphereRising = dataElement.getChild("yweather:atmosphere").getAttribute("rising").getValue();
		
		astronomySunrise = dataElement.getChild("yweather:astronomy").getAttribute("sunrise").getValue();
		astronomySunset = dataElement.getChild("yweather:astronomy").getAttribute("sunset").getValue();
		
		itemGeoLAT = dataElement.getChild("item").getChild("geo:lat").getValue();
		itemGeoLONG = dataElement.getChild("item").getChild("geo:long").getValue();
		itemPubDate = dataElement.getChild("item").getChild("pubDate").getValue();
		
		itemConditionsCode = dataElement.getChild("item").getChild("yweather:condition").getAttribute("code").getValue();
		itemConditionsTemperature = dataElement.getChild("item").getChild("yweather:condition").getAttribute("temp").getValue();
		itemConditionsDate = dataElement.getChild("item").getChild("yweather:condition").getAttribute("date").getValue();
		
//		Element foreCast = dataElement.getChild("item").getChild("yweather:forecast");
	}

Und hier die erhaltene RSS/XML Datei:

[XML]<?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 - Sunnyvale, CA</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
<description>Yahoo! Weather for Sunnyvale, CA</description>
<language>en-us</language>
<lastBuildDate>Sat, 19 Mar 2011 3:56 pm PDT</lastBuildDate>
<ttl>60</ttl>

<yweather:location city="Sunnyvale" region="CA" country="United States"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="51" direction="150" speed="10" />
<yweather:atmosphere humidity="" visibility="7" pressure="29.8" rising="0" />
<yweather:astronomy sunrise="7:12 am" sunset="7:19 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 Sunnyvale, CA at 3:56 pm PDT</title>
<geo:lat>37.37</geo:lat>
<geo:long>-122.04</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
<pubDate>Sat, 19 Mar 2011 3:56 pm PDT</pubDate>
<yweather:condition text="Light Rain" code="11" temp="51" date="Sat, 19 Mar 2011 3:56 pm PDT" />
<description><![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/11.gif"/><br />
<b>Current Conditions:</b><br />
Light Rain, 51 F<BR />
<BR /><b>Forecast:</b><BR />
Sat - Rain/Wind. High: 53 Low: 50<br />
Sun - Rain. High: 56 Low: 45<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_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="Sat" date="19 Mar 2011" low="50" high="53" text="Rain/Wind" code="12" />
<yweather:forecast day="Sun" date="20 Mar 2011" low="45" high="56" text="Rain" code="12" />
<guid isPermaLink="false">USCA1116_2011_03_19_15_56_PDT</guid>

</item>
</channel>
</rss>
<!-- api1.weather.ch1.yahoo.com compressed/chunked Sat Mar 19 17:06:19 PDT 2011 -->
[/XML]

Ich find den Fehler nicht... 🙁
 
Java:
dataElement.getChild("yweather:location")

das liefert eben null, weil du das so nicht angeben kannst.
Java:
dataElement.getChild("location", Namespace.getNamespace("http://xml.weather.yahoo.com/ns/rss/1.0"))

probiere es mal so, das musst du dann natürlich überall so machen. Da der Namespace aber für die meisten gleich ist, würde es auch reichen den Namespace einmal zu holen.
Java:
        /*Namespace */
        Namespace ns = Namespace.getNamespace("http://xml.weather.yahoo.com/ns/rss/1.0");
        /* location info */
       String  locationCity = dataElement.getChild("location", ns).getAttribute("city").getValue();
        String  locationRegion = dataElement.getChild("location", ns).getAttribute("region").getValue();
        String  locationCountry = dataElement.getChild("location", ns).getAttribute("country").getValue();
        
        /* unit info */
        String unitTemperature = dataElement.getChild("units" ,ns).getAttribute("temperature").getValue();
        String  unitDistance = dataElement.getChild("units" ,ns).getAttribute("distance").getValue();
        String  unitPressure = dataElement.getChild("units" ,ns).getAttribute("pressure").getValue();
        String  unitSpeed = dataElement.getChild("units" ,ns).getAttribute("speed").getValue();

usw....eben aus allen wo bei dir yweather davor steht, abändern -->
getChild("yweather:wind")
-->
getChild("wind", ns)

😉
 
Was ist eigentlich dieser Namespace?

Ich bin nun beim "Item" Element angelangt und habe dort auch den NAMESPACE eingesetzt, bekomme aber wieder eine nullpointer exception, ich habe via google probiert irgendwas ausfindig zu machen, aber zu dem thema findet man nichts?! Muss ich dort nun nen anderen Namespace einsetzten?

Java:
public void getRequestData(Element root) {
		
		Element dataElement = root.getChild("channel");
		Namespace space = Namespace.getNamespace("http://xml.weather.yahoo.com/ns/rss/1.0");
		/* general info */
		title = dataElement.getChild("title").getValue();
		description = dataElement.getChild("description").getValue();
		
		/* location info */
		locationCity = dataElement.getChild("location", space).getAttribute("city").getValue();
		locationRegion = dataElement.getChild("location", space).getAttribute("region").getValue();
		locationCountry = dataElement.getChild("location", space).getValue();
		
		/* unit info */
		unitTemperature = dataElement.getChild("units", space).getAttribute("temperature").getValue();
		unitDistance = dataElement.getChild("units", space).getAttribute("distance").getValue();
		unitPressure = dataElement.getChild("units", space).getAttribute("pressure").getValue();
		unitSpeed = dataElement.getChild("units", space).getAttribute("speed").getValue();
		
		/* wind info */
		windChill = dataElement.getChild("wind", space).getAttribute("chill").getValue();
		windDirection = dataElement.getChild("wind", space).getAttribute("direction").getValue();
		windSpeed = dataElement.getChild("wind", space).getAttribute("speed").getValue();
		
		/* atmosphere info */
		atmosphereHumidity = dataElement.getChild("atmosphere", space).getAttribute("humidity").getValue();//feuchtigkeit
		atmosphereVisibility = dataElement.getChild("atmosphere", space).getAttribute("visibility").getValue();
		atmospherePressure = dataElement.getChild("atmosphere", space).getAttribute("pressure").getValue(); 
		atmosphereRising = dataElement.getChild("atmosphere", space).getAttribute("rising").getValue();
		
		astronomySunrise = dataElement.getChild("astronomy", space).getAttribute("sunrise").getValue();
		astronomySunset = dataElement.getChild("astronomy", space).getAttribute("sunset").getValue();
		
		itemGeoLAT = dataElement.getChild("item").getValue();//getChild("geo:lat", space).getValue();
		System.out.println(itemGeoLAT);
		itemGeoLONG = dataElement.getChild("item").getChild("geo:long").getValue();
		itemPubDate = dataElement.getChild("item").getChild("pubDate").getValue();
		
		itemConditionsCode = dataElement.getChild("item", space).getChild("yweather:condition").getAttribute("code").getValue();
		itemConditionsTemperature = dataElement.getChild("item").getChild("yweather:condition").getAttribute("temp").getValue();
		itemConditionsDate = dataElement.getChild("item").getChild("yweather:condition").getAttribute("date").getValue();
		
//		Element foreCast = dataElement.getChild("item").getChild("yweather:forecast");
	}

Wäre nett wenn ihr mir helfen könntet.
 
Zuletzt bearbeitet:
So schwer ist das nicht, du musst nur schauen was eben in deiner XML-Datei steht, siehe
[xml]
<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#">
[/xml]

Wie du siehst, ist hier geo = http://www.w3.org/2003/01/geo/wgs84_pos#" definiert, d.h. nun wieder, anstelle von
Java:
dataElement.getChild("item").getChild("geo:long").getValue();
schreibst du
Java:
dataElement.getChild("item").getChild("long", Namespace.getNamespace("http://www.w3.org/2003/01/geo/wgs84_pos#")).getValue();

bzw. definierst vorher wieder einen neuen Namespace, mit der anderen URI !

mavinatic hat gesagt.:
Was ist eigentlich dieser Namespace?
Namensraum (XML) ? Wikipedia
 

Neue Themen


Zurück
Oben