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

时间:2019-05-15 09:58:33

标签: xml applescript

我有一个Applescript,它接收格式化为参数格式的XML字符串,并且我想读取/解析数据。收到的字符串如下:

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

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

set theName to _XML_element_name_
set theTarget to _XML_element_target_
...

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

$(window).resize(function() {
  if ($(window).width() < 960) {
    function openNav() {
      document.getElementById("mySidebar").style.width = "70%";
    }

    function closeNav() {
      document.getElementById("mySidebar").style.width = "0%";
      document.getElementById("main").style.paddingLeft = "0";
      document.getElementById("table-name").style.marginRight = "0";
      document.getElementById("table-name").style.marginLeft = "0";
    }
  } else if ($(window).width() > 960) {
    function openNav() {
      document.getElementById("mySidebar").style.width = "14%";
      document.getElementById("main").style.paddingLeft = "13%";
      document.getElementById("table-name").style.marginRight = "28px";
      document.getElementById("table-name").style.marginLeft = "26px";
    }

    function closeNav() {
      document.getElementById("mySidebar").style.width = "0";
      document.getElementById("main").style.paddingLeft = "0";
      document.getElementById("table-name").style.marginRight = "0";
      document.getElementById("table-name").style.marginLeft = "0";
    }
  }
});

1 个答案:

答案 0 :(得分:1)

只需通过纯文本创建一个新的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