使用 AppleScript 通过 A URL 解析 XML

Use AppleScript To Parse XML Via A URL

我正在尝试解析来自网络媒体播放器的数据。

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

http://192.168.10.2:8008/GetUserVars

本returns以下数据:-

<?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 从 BrightSignVar XML 元素节点获取值,其 name 属性为 CurrentLetterCurrentUserName

本质上,我想获取这两个文本节点:

我试过下面的例子但是有错误,有什么想法吗?

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

这个 returns 响应如上,但是我在尝试将 CurrUser 设置为变量时出现错误:

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

你们非常亲密。这是你的最后一个脚本,我认为应该进行一些适当的修改:

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 响应文本时,return 值为:

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