如何使用 AppleScript 获取和解析 XML 文件?
某个远程服务器上有一个 XML 文件 (http://foo/bar.xml):
<?xml version="1.0" encoding="UTF-8"?>
<foo>
bar
</foo>
如何使用 AppleScript 获取值“bar”?
There's an XML file on some remote server (http://foo/bar.xml):
<?xml version="1.0" encoding="UTF-8"?>
<foo>
bar
</foo>
How can I get the value "bar" using AppleScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我所做的:
最初我使用“URL Access Scripting”应用程序来获取文件,但由于它已在 Lion 中删除,所以我切换到纯卷曲,它在 Snow Leopard 和 Lion 下都可以工作。
Here's what I've done:
Originally I was using the "URL Access Scripting" app to fetch the file, but since it has been removed in Lion, I switched to pure curl, which works under both Snow Leopard and Lion.
我发现 此线程 其中有一个解析 XML 文件的示例使用通过系统事件提供的 XML 工具。但对我来说似乎很复杂。
还有这个(免费软件)脚本附加包用于解析/写入 XML。没看过,不过应该还不错吧
就个人而言,我会将脚本保存为脚本包,然后制作一些 php/Ruby/perl/python/任何脚本来解析包中的 XML(因为我对此更满意)。然后我会使用 AppleScript 将 XML 从 cURL 传递到解析器脚本。
AppleScript:
parse_xml.rb 可能是这样的(以 Ruby 为例):(
Ruby 和 REXML 包应该可以在任何 Mac 上使用,所以它应该可以在任何地方工作......我相信)
重点是,当脚本运行它时'我们将使用 cURL 下载 XML 文件,并将其传递给 Ruby 脚本,最后,AppleScript 中的
fooValue
将被设置为“bar”。当然,如果 XML 更复杂,您将需要更多脚本,或者再看看其他选项。
可能还有更多的方法可以做到这一点(例如,您可以只进行一些字符串操作,而不是完整的 XML 解析,但这当然有点脆弱),但我就到此为止:)
I found this thread which has an example of parsing an XML file with the XML tools available via System Events. Seems pretty convoluted to me though.
There's also this (freeware) scripting addition package for parsing/writing XML. Haven't looked at it, but it might be neat.
Personally, I would save my script as a script bundle, and then I'd make a little php/Ruby/perl/python/whatever script to parse the XML (since I'm just more comfortable with that) in the bundle. Then I'd use AppleScript then pass the XML to the parser script from cURL.
AppleScript:
parse_xml.rb could be somthing like this (using Ruby as an example):
(Ruby and the REXML package should be readily available on any Mac, so it should work anywhere… I believe)
Point is, when the script runs it'll download the XML file with cURL, pass it to the Ruby script, and in the end,
fooValue
in the AppleScript will be set to "bar".Of course, if the XML is more complex, you'll need more scripting, or take another look at the other options.
There are probably even more ways of doing it (for instance, you could just do some string manipulation instead of full-on XML parsing, but that's a little brittle of course), but I'll stop here :)
意识到这是一个老问题,但这是使用 Bing Maps API 的一种方法(请注意,我将 API 密钥替换为
XXXXXXXXXX
)。使用do shell script
和curl
来检索 XML,然后沿着元素向下查找,直到找到所需的元素(您可以合并所有tell< /code>s 到
告诉 xml 元素“X”的 xml 元素“y”的 xml 元素...
,但这更容易理解)。编辑:我想我应该包含我用于上述内容的 XML:
Realizing this is an old question, but here’s one way using the Bing Maps API (note I replaced my API key with
XXXXXXXXXX
). usedo shell script
withcurl
to retrieve the XML, then just walk down the elements until you get to the one you need (you can consolidate all thetell
s intotell xml element “X” of xml element “y” of xml element…
, but this is just easier to follow).EDIT: I suppose I should include the XML I was using for the above: