如何从AppleScript中的字符串(而非文件)创建XML



我有一个applescript,它接收一个格式为参数的XML字符串,我想读取/解析数据。接收的字符串看起来像:

<?xml version="1.0" encoding="utf-8" ?>
  <win>
  <name>Documents</name>
  <target>SSD:Documents:</target>
  <listview>1</listview>
  <sidebar>0</sidebar>
  <bounds>1200, 630, 1825, 1080</bounds>
</win>

我看到了带有XML文件的示例,但我不知道如何根据接收的字符串填充记录。最终记录可能是这样:

set myRecord to {name:_XML_element_name_, tg:_XML_element_target_, listview: ...}

,或者我至少可以使用一些变量:

set theName to _XML_element_name_
set theTarget to _XML_element_target_
...

只需从纯文本制作一个新的 XML data实例

set xmlText to "<?xml version="1.0" encoding="utf-8" ?>
  <win>
  <name>Documents</name>
  <target>SSD:Documents:</target>
  <listview>1</listview>
  <sidebar>0</sidebar>
  <bounds>1200, 630, 1825, 1080</bounds>
</win>"
tell application "System Events"
    set xmlData to make new XML data with properties {text:xmlText}
    tell XML element "win" of xmlData
        set theName to value of XML element "name"
        set theTarget to value of XML element "target"
    end tell
end tell

最新更新