如何从 Applescript 中的字符串(不是文件)创建 XML? [英] How to create XML from a string (not a file) in Applescript?

查看:27
本文介绍了如何从 Applescript 中的字符串(不是文件)创建 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Applescript that receive an XML string formatted as parameter, and I would like to read/parse data. Received string looks like:

<?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 文件的示例,但我不知道如何根据收到的字符串填充记录.最终记录可能是这样的:

I saw examples with XML files but I can't figure how to populate a record based upon received string. Final record could be like this:

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

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

Or I could use at least some variables:

set theName to _XML_element_name_
set theTarget to _XML_element_target_
...

推荐答案

只需从纯文本中创建一个新的 XML 数据 实例

Just make a new XML data instance from the plain text

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

这篇关于如何从 Applescript 中的字符串(不是文件)创建 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆