Xpath HELP

Post your questions and help other users.

Moderator: Martin

Post Reply
bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Xpath HELP

Post by bogdyro » 27 Feb 2016 06:41

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.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Xpath HELP

Post by Martin » 27 Feb 2016 07:01

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

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Xpath HELP

Post by bogdyro » 27 Feb 2016 07:45

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.

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Xpath HELP

Post by bogdyro » 27 Feb 2016 08:30

Works here
http://codebeautify.org/Xpath-Tester
Also WORKS in automagic.
Thanks Martin !

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Xpath HELP

Post by Martin » 27 Feb 2016 09:50

Glad it's working. Many online XPath testers don't support namespaces. I'll definitely bookmark the one from codebeautify. Thanks for the link!

jmckeejr
Posts: 34
Joined: 03 May 2015 12:29

Re: Xpath HELP

Post by jmckeejr » 16 Feb 2019 23:36

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 700 times

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Xpath HELP

Post by Desmanto » 17 Feb 2019 10:02

Do you mean the high in this?

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>
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.

Code: Select all

tempf = evaluateXPathAsString(xml, "/response/forecast/simpleforecast/forecastdays/forecastday/high/fahrenheit");
BTW, if the server support json version, it is better to use JSON. As currently, Automagic can handle json much better than xml.
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.

jmckeejr
Posts: 34
Joined: 03 May 2015 12:29

Re: Xpath HELP

Post by jmckeejr » 17 Feb 2019 22:19

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

Post Reply