问题
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