Automator - 将 URL 中的 XML 解析为变量(或字典)

发布于 2025-01-11 19:01:09 字数 1514 浏览 0 评论 0原文

第一篇文章在这里!

我知道如何编写一些代码,但请把我视为一个完全的新手。

我正在尝试执行一个 Automator 操作(Mac),从 URL(URL 永远不会改变)读取 XML 并解析它。

XML 包含大量数据,但我感兴趣的是两个特定值:“名称”和特定于该名称的存储的“URL”。

这是 XML 的示例,如下所示:

<jobs>
    <jobad>
        <id>54844</id>
        <title>HR Manager</title>
        <text>Random text</text>
        <location>City, State</location>
        <expiration/>
        <url>https://url/jobs/43395.58958.54844</url>
        <application>
            https://url/jobs/43395.58958.54844.1
        </application>
</jobad>
<jobs>
    <jobad>
        <id>54847</id>
        <title>Sales Manager</title>
        <text>Random text</text>
        <location>City, State</location>
        <expiration/>
        <url>https://url/jobs/43395.58958.54847</url>
        <application>
            https://url/jobs/43395.58958.54847.1
        </application>
</jobad>

我不需要大部分信息。我基本上只需要 </code> 和 <code><url></code>。

这个想法是解析 XML,将其添加到字典中,我可以在其中创建一个菜单,显示所有 </code> 值,让用户选择他们想要的值,然后返回 <所选 <code><title></code> 的 code><url></code>。

我尝试以不同的方式解析但失败了,因为我不太了解 applescript。另外,它不一定是字典,任何能完成这项工作的变量都可以。这就是我尝试过的。

我确实已经启动并运行了菜单的代码以及所选项目的返回。

XML 解析让我头疼。

提前致谢!

First post here!

I know how to code a little, but please regard me as a total newb.

I'm trying to do an Automator action (Mac) that reads a XML from a URL (URL never changes) and parses it.

The XML has a lot of data, but my interest is in two specific values, the "Name" and a stored "URL" specific for that name.

This is an example of the XML looks like:

<jobs>
    <jobad>
        <id>54844</id>
        <title>HR Manager</title>
        <text>Random text</text>
        <location>City, State</location>
        <expiration/>
        <url>https://url/jobs/43395.58958.54844</url>
        <application>
            https://url/jobs/43395.58958.54844.1
        </application>
</jobad>
<jobs>
    <jobad>
        <id>54847</id>
        <title>Sales Manager</title>
        <text>Random text</text>
        <location>City, State</location>
        <expiration/>
        <url>https://url/jobs/43395.58958.54847</url>
        <application>
            https://url/jobs/43395.58958.54847.1
        </application>
</jobad>

I don't need most of the information. I basically just need the <title> and the <url>.

The idea is to parse the XML, add it into a dictionary where I can make a menu, that shows all the <title> values, have the user select which one they want, and return the <url> of the selected <title>.

I've tried to parse on different ways but failed because I don't know applescript that well. Also, it doesn't have to be a dictionary, any variable that does the job will do. This is just what I've tried.

I do have the code up and running for the menu and the return of the selected item.

It is the XML parsing that's breaking my head.

Thanks in advace!

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

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

发布评论

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

评论(1

零度℉ 2025-01-18 19:01:09

好吧,首先,您的示例 XML 格式错误,jobs 标签未关闭。我假设是这样并在桌面上保存为“Jobs.xml”:

<jobs>
    <jobad>
        <id>54844</id>
        <title>HR Manager</title>
        <text>Random text</text>
        <location>City, State</location>
        <expiration/>
        <url>https://url/jobs/43395.58958.54844</url>
        <application>
            https://url/jobs/43395.58958.54844.1
        </application>
    </jobad>
    <jobad>
        <id>54847</id>
        <title>Sales Manager</title>
        <text>Random text</text>
        <location>City, State</location>
        <expiration/>
        <url>https://url/jobs/43395.58958.54847</url>
        <application>
            https://url/jobs/43395.58958.54847.1
        </application>
    </jobad>
</jobs>

脚本可能是:

tell application "System Events"
    tell XML file "~/Desktop/Jobs.xml"
        tell XML element "jobs"
            set myJobs to {}
            set jobads to every XML element whose name = "jobad"
            repeat with thisJOb in jobads
                set myTitle to (value of first XML element of thisJOb whose name = "title")
                set myUrl to (value of first XML element of thisJOb whose name = "url")
                set the end of myJobs to {myTitle, myUrl}
            end repeat
        end tell
    end tell
end tell
return myJobs

结果是:

{{"HR Manager", "https://url/jobs/43395.58958.54844"}, {"Sales Manager", "https://url/jobs/43395.58958.54847"}}

OK then, first, your example XML is malfomed, the jobs tag doesn't close. I assume it's this way and saved on the desktop as "Jobs.xml" :

<jobs>
    <jobad>
        <id>54844</id>
        <title>HR Manager</title>
        <text>Random text</text>
        <location>City, State</location>
        <expiration/>
        <url>https://url/jobs/43395.58958.54844</url>
        <application>
            https://url/jobs/43395.58958.54844.1
        </application>
    </jobad>
    <jobad>
        <id>54847</id>
        <title>Sales Manager</title>
        <text>Random text</text>
        <location>City, State</location>
        <expiration/>
        <url>https://url/jobs/43395.58958.54847</url>
        <application>
            https://url/jobs/43395.58958.54847.1
        </application>
    </jobad>
</jobs>

and the script might be :

tell application "System Events"
    tell XML file "~/Desktop/Jobs.xml"
        tell XML element "jobs"
            set myJobs to {}
            set jobads to every XML element whose name = "jobad"
            repeat with thisJOb in jobads
                set myTitle to (value of first XML element of thisJOb whose name = "title")
                set myUrl to (value of first XML element of thisJOb whose name = "url")
                set the end of myJobs to {myTitle, myUrl}
            end repeat
        end tell
    end tell
end tell
return myJobs

the result is :

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