Hello
So I have this XML response from yahoo weather API. I have read some xPath tutorials, tried different ways but I really cannot extract any info from it. Any help please?
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
yahoo:count="1" yahoo:created="2016-02-27T05:47:36Z" yahoo:lang="ro-RO">
<results>
<channel>
<item>
<yweather:condition
xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
code="27" date="Fri, 26 Feb 2016 8:50 pm PST"
temp="63" text="Mostly Cloudy"/>
</item>
</channel>
</results>
</query>
How to get 'date', 'temp', 'text' ?
Thanks.
Xpath HELP
Moderator: Martin
Re: Xpath HELP
Hi,
Following script should work to extract the temperature:
temp = evaluateXPathAsString(xml, "/query/results/channel/item/yweather:condition/@temp");
or alternatively:
temp = evaluateXPathAsString(xml, "//yweather:condition/@temp");
There are also some examples available here: Script examples (scroll down to title XML)
Regards,
Martin
Following script should work to extract the temperature:
temp = evaluateXPathAsString(xml, "/query/results/channel/item/yweather:condition/@temp");
or alternatively:
temp = evaluateXPathAsString(xml, "//yweather:condition/@temp");
There are also some examples available here: Script examples (scroll down to title XML)
Regards,
Martin
Re: Xpath HELP
Hi Martin. Thanks for the quick reply. Actually none of them work. Tried 2 online evaluators
http://www.utilities-online.info/xpath/#.VtFT-dBaz5c
http://www.freeformatter.com/xpath-tester.html
No text returned. Weird.
http://www.utilities-online.info/xpath/#.VtFT-dBaz5c
http://www.freeformatter.com/xpath-tester.html
No text returned. Weird.
Re: Xpath HELP
Glad it's working. Many online XPath testers don't support namespaces. I'll definitely bookmark the one from codebeautify. Thanks for the link!
Re: Xpath HELP
This one a bit harder. I can get the icon for period 1 but can't get the high in fahrenheit. Can anyone help please
- Attachments
-
- weather.xml
- (10.73 KiB) Downloaded 708 times
Re: Xpath HELP
Do you mean the high in this?
I just open the xml in notepad++ and use the xml plugin. Click at <fahrenheit>36</fahrenheit> and use the XML Tools > current XML Path. It shows
/response/forecast/simpleforecast/forecastdays/forecastday/high/fahrenheit
You should can get it by using.
BTW, if the server support json version, it is better to use JSON. As currently, Automagic can handle json much better than xml.
Code: Select all
</date>
<period>1</period>
<high>
<fahrenheit>36</fahrenheit>
<celsius>2</celsius>
</high>
<low>
<fahrenheit>18</fahrenheit>
<celsius>-8</celsius>
</low>
<conditions>Clear</conditions>
<icon>clear</icon>
<icon_url>http://icons.wxug.com/i/c/k/clear.gif</icon_url>
<skyicon></skyicon>
/response/forecast/simpleforecast/forecastdays/forecastday/high/fahrenheit
You should can get it by using.
Code: Select all
tempf = evaluateXPathAsString(xml, "/response/forecast/simpleforecast/forecastdays/forecastday/high/fahrenheit");
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Re: Xpath HELP
Thanks I don't get on my laptop often, so I found this online tool that helped a lot and now I have a pretty sweet set of weather widgets.
I used https://xmltoolbox.appspot.com/xpath_generator.html
I used https://xmltoolbox.appspot.com/xpath_generator.html