使用 Applescript 获取 xml 属性的值

发布于 2024-08-28 02:55:28 字数 592 浏览 5 评论 0原文

我想解析 Yahoo! 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" />

我怎样才能用苹果脚本做到这一点?

所以,

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

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

我希望预测位于某种数组中,以便按日期/日期组织它们

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上青苔 2024-09-04 02:55:28

系统事件有一小部分本机命令可以帮助您解决问题(未经测试):

(*
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解决方案,我只知道来自Late Night Software的XML工具添加(免费)和Satimage 与其套件捆绑了更复杂的套件< /a> (付费),但我对此没有任何经验。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文