2010-04-07 131 views
2

我想解析Yahoo!天氣API,我想保存這些元素的屬性,以供日後使用變量:使用Applescript獲取xml屬性的值

<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" /> 

我怎樣才能做到這一點使用AppleScript?

所以,

set locationCity to [city] 
set locationRegion to [reagion] 
set locationCountry to [country] 

set astronomySunrise to [sunriseTime] 
set astronomySunset to [sunsetTime] 

我想forcasts是在某種白天/日期來組織他們

回答

2

系統事件的數組有一小部分,可以讓你周圍的原生指令(未經測試):

(* 
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 

從我所收集的AppleScript的XML掛鉤是非常挑剔它的XML和比「的一些數據排查其他的方式並沒有提供多少不是的預期類型「。

我去其他地方爲我的XML解析,我建議在這裏相同。如果需要純粹的Applescript解決方案,我只知道XML Tools addition from Late Night Software(免費)和Satimage has a more complex set bundled with their suite(付費),但我沒有任何經驗。