Reputation: 853
I want to parse the Yahoo! Weather API and I want to save these element attributes to variables for later use:
<yweather:location city="Zebulon" region="NC" country="US"/>
<yweather:astronomy sunrise="6:52 am" sunset="7:39 pm"/>
<yweather:forecast day="Wed" date="7 Apr 2010" low="61" high="96" text="Partly Cloudy" code="29" />
How can I do this with applescript?
So,
set locationCity to [city]
set locationRegion to [reagion]
set locationCountry to [country]
set astronomySunrise to [sunriseTime]
set astronomySunset to [sunsetTime]
I want the forcasts to be in an array of some sort to organize them by day/date
Upvotes: 2
Views: 2559
Reputation: 5055
System Events has a small set of native commands that can get you around (untested):
(*
Assuming the following dummy XML:
<?xml version="1.0" encoding="UTF-8"?>
<RootTag>
<yweather:location city="Zebulon" region="NC" country="US"/>
<yweather:astronomy sunrise="6:52 am" sunset="7:39 pm"/>
<yweather:forecast day="Wed" date="7 Apr 2010" low="61" high="96" text="Partly Cloudy" code="29" />
</RootTag>
*)
set theXMLFile to ((choose file) as string)
tell application "System Events"
set theXMLData to contents of XML file theXMLFile
tell theXMLData
set theXMLRoot to XML element 1
set locationTag to XML element 1 of theXMLRoot
set cityAttribute to XML attribute 1 of locationTag
return cityAttribute --> "Zebulon"
end tell
end tell
From what I gather Applescript's XML hooks are very finicky about it's XML and doesn't offer much in the way of troubleshooting other than "some data not of the expected type".
I go elsewhere for my XML parsing and I recommend the same here. If a pure Applescript solution is required, I only know of the XML Tools addition from Late Night Software (free) and Satimage has a more complex set bundled with their suite (paid), but I don't have any experience with either.
Upvotes: 2