如何从 Applescript 中的字符串(不是文件)创建 XML?

编程入门 行业动态 更新时间:2024-10-19 20:38:16
本文介绍了如何从 Applescript 中的字符串(不是文件)创建 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 14:12:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   文件   Applescript   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!