2010-04-07 155 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(付费),但我没有任何经验。