使用Applescript获取xml属性的值

使用Applescript获取xml属性的值,xml,parsing,applescript,Xml,Parsing,Applescript,我想解析雅虎!Weather API和我希望将这些元素属性保存到变量中,以供以后使用: <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" tex

我想解析雅虎!Weather API和我希望将这些元素属性保存到变量中,以供以后使用:

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

我希望Forcast以某种形式排列,以便按天/日期对其进行组织。System Events(系统事件)有一小部分本机命令,可供您使用(未经测试):

(*
假设以下虚拟XML:
*)
将XmlFile设置为((选择文件)作为字符串)
告诉应用程序“系统事件”
将XmlData设置为XML文件的内容XmlFile
告诉他们XML数据
将XmlRoot设置为XML元素1
将locationTag设置为XmlRoot的XML元素1
将cityAttribute设置为locationTag的XML属性1
返回城市属性-->“泽布隆”
结束语
结束语
据我所知,Applescript的XML钩子对它的XML非常挑剔,除了“某些数据不是预期的类型”之外,在故障排除方面没有提供太多帮助

我去了其他地方进行XML解析,这里我推荐同样的方法。如果需要纯Applescript解决方案,我只知道(免费)和(付费),但我对两者都没有任何经验

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

set astronomySunrise to [sunriseTime]
set astronomySunset to [sunsetTime]
(*
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