How to create XML from a string (not a file) in Applescript?

故事扮演 提交于 2021-01-28 11:50:35

问题


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>

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_
...

回答1:


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


来源:https://stackoverflow.com/questions/56146576/how-to-create-xml-from-a-string-not-a-file-in-applescript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!