使用AppleScript通过URL解析XML [英] Use AppleScript To Parse XML Via A URL

查看:122
本文介绍了使用AppleScript通过URL解析XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网络媒体播放器中解析数据.

I am trying to parse data from a networked media player.

我可以通过以下端点访问播放器上的数据:

I can access the data on the player via the following endpoint:

http://192.168.10.2:8008/GetUserVars

这将返回以下数据:-

<?xml version="1.0" encoding="UTF-8"?>
<BrightSignUserVariables>
  <BrightSignVar name="CurrentLetter">W</BrightSignVar>
  <BrightSignVar name="CurrentUserName">&amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W</BrightSignVar>
</BrightSignUserVariables>

由此,我需要使用AppleScript从name属性为CurrentLetterCurrentUserNameBrightSignVar XML元素节点获取值.

From this I need to use AppleScript to get the values from the BrightSignVar XML element node whose name attribute is CurrentLetter and CurrentUserName.

基本上,我想获得以下两个文本节点:

Essentially, I want to obtain these two text nodes:

  • W
  • &amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W
  • W
  • &amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W&amp;Q&amp;W

我尝试了以下示例,但遇到错误,有任何想法吗?

I tried the following example but got errors, any ideas?

set theXMLFile to ((http://192.168.10.2:8008/GetUserVars) as string)

tell application "System Events"

    set CurrUser to (value of XML element "CurrentUserName")

    set CurrLetter to (value of XML element "CurrentLetter")

end tell


我也尝试了以下方法:

set theXMLFile to (do shell script (("curl -X GET 'http://192.168.10.5:8008/GetUserVars'")))

tell application "System Events"

    set CurrUser to XML elements of XML element "BrightSignVar" of XML element "BrightSignUserVariables" of theXMLFile whose name is "CurrentUserName"

end tell

这将返回上述响应,但是尝试将CurrUser设置为变量时出现错误:

This returns the response as above however I get an error when trying to set CurrUser as variable:

can't make "BrightSignUserVariables" into type integer (-1700)

推荐答案

您非常亲密.这是您的最后一个脚本,其中包含一些我认为应该可行的修正:

You were very close. Here's your last script with a few appropriate amendments that I believe should work:

set userVars to do shell script "curl -X GET 'http://192.168.10.5:8008/GetUserVars'"

tell application id "com.apple.SystemEvents"
    tell (make new XML data with data userVars)
        tell XML element "BrightSignUserVariables"
            tell every XML element whose name="BrightSignVar" to ¬
                set {letter, username} to its value
        end tell
    end tell
end tell

当我使用您提供的XML响应文本运行它时,返回值为:

When I ran it using the XML response text you provided, the return value was:

{"W", "&Q&W&Q&W&Q&W&Q&W"}

这篇关于使用AppleScript通过URL解析XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆