简体   繁体   English

使用Apple Script解析XML

[英]Parse XML with Apple Script

I try to create an Automator workflow which download images from a website. 我尝试创建一个从网站下载图像的Automator工作流程。 The name of the images are in a xml file and I want to extract them in order to create an url like http://test.com/images/dynamic_image_name.jpg 图片的名称位于xml文件中,我想提取它们以创建类似http://test.com/images/dynamic_image_name.jpg的网址

I found how to download the images when I have the URL with Automator but I'm looking for a way to parse the XML file in order to extract the images name and generate automatically the good URLs. 我发现使用Automator拥有URL时如何下载图像,但是我正在寻找一种解析XML文件的方法,以提取图像名称并自动生成良好的URL。

Here is a part of the XML file : 这是XML文件的一部分:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Managers</key>
<dict>
    <key>Sophie Barriac</key>
    <dict>
        <key>image</key>
        <string>sophie.png</string>
        <key>Téléphone</key>
        <dict>
            <key>number</key>
            <string>0460046150</string>
            <key>mobile</key>
            <string>0614589665</string>
        </dict>
        <key>Email</key>
        <dict>
            <key>email</key>
            <string>sophie.barriac@cgi.com</string>
        </dict>
    </dict>
    <key>Kevin Berthier</key>
    <dict>
        <key>image</key>
        <string>kevin.png</string>
        <key>Téléphone</key>
        <dict>
            <key>number</key>
            <string>0469646007</string>
        </dict>
        <key>Email</key>
        <dict>
            <key>email</key>
            <string>kevin.berthier@cgi.com</string>
        </dict>
    </dict>
</dict>

I saw some things about AppleScript but I know nothing about this. 我看到了有关AppleScript的一些信息,但对此一无所知。 How can I do this ? 我怎样才能做到这一点 ?

Since the XML file you posted is a .plist file, you can use something like this: 由于您发布的XML文件是.plist文件,因此您可以使用以下代码:

tell application "System Events"
    set plistFile to contents of property list file "PATH TO PLIST FILE"

    set managersPlist to property list item "Managers" of plistFile
    set managers to every property list item of managersPlist

    repeat with manager in managers
        set imageFile to value of property list item "image" of manager
        display dialog imageFile
    end repeat
end tell

This will extract all image keys' values. 这将提取所有image键的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM