I’ve got some xml data in a variable, not a file. Been trying to parse it without success. Here’s an example:
set TheXml to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<theapp>
<version>1</version>
</theapp >" as Unicode text
tell application "System Events"
set TheXml to XML data
set TheData to XML elements of TheXml
end tell
Any idea anyone? How could I just get to the data 1 in the Attribute “version”?
the XML commands of System Events work only reading the data from a file.
I recommend to save your text temporarily into a file and then parse the data
like this
set tempFile to (((path to desktop) as Unicode text) & "XMLtemp.txt")
set TheXml to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<theapp>
<version>1</version>
</theapp >" as Unicode text
set ff to open for access file tempFile with write permission
write TheXml to ff
close access ff
tell application "System Events"
tell contents of XML file tempFile
set TheData to XML elements
end tell
end tell
set del to button returned of (display dialog "Delete \"XMLtemp.txt\" immediately?" buttons {"Cancel", "Delete"})
if del is "Delete" then do shell script "rm " & quoted form of POSIX path of tempFile
really? the variable theData contains the XML element(s) of the file.
try this
set tempFile to (((path to desktop) as Unicode text) & "XMLtemp.txt")
set TheXml to "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<theapp>
<version>1</version>
</theapp >" as Unicode text
set ff to open for access file tempFile with write permission
write TheXml to ff
close access ff
tell application "System Events"
tell contents of XML file tempFile
set TheData to XML element 1 of XML element 1
tell TheData to set {theName, theValue} to {name, value}
end tell
end tell
display dialog theName & ": " & theValue
--> version: 1
set del to button returned of (display dialog "Delete tempFile immediately?" buttons {"Cancel", "Delete"})
if del is "Delete" then do shell script "rm " & quoted form of POSIX path of tempFile