'Automator - Parsing XML in URL to variable (or dictionary)

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!



Solution 1:[1]

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"}}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chino22