I have xml-file. When i open it in PlistEdit Pro it has “folders” i think these are called dictionary or array.
Because i really can’t figure out how to read xml which has information in multiple “folders” deep (for instance Jobs-array in Project-dictionary), i would like to convert this xml-data to normal applescript list or record.
Here’s how I would do it with a plist file. I’m sure an xml file is the same or at least similar… you just get the “value” of each item, which can be done in one step, and you get back an applescript array (or dictionary depending on the plist type) with all the values.
set plistpath to choose file
tell application "System Events"
set plistFile to property list file (plistpath as text)
set plistItems to value of property list items of plistFile
end tell
return plistItems
error “System Events got an error: Can’t get contents of property list file "Macintosh HD:Users:cirno:Desktop:text.xml".” number -1728 from contents of property list file “Macintosh HD:Users:cirno:Desktop:text.xml”
I mentioned it might not work with xml, however system events also handles xml as well as plist files. The two are very similar so all you need to do is look at system event’s applescript dictionary for xml and check the commands… they should be similar to what I showed you.
Don’t have time to work out the exact coding for you, but two thoughts:
The late night software tool posted above is really good, especially if you want to write updates back out to an XML file.
You can do basic reading of XML files using System Events. See the system events dictionary:
XML Suite Terms and Events for accessing the content of XML files
XML attribute‚n [inh. item] : A named value associated with a unit of data in XML format
elements
contained by XML elements.
properties
name (text, r/o) : the name of the XML attribute
value (any) : the value of the XML attribute
XML data‚n [inh. item] : Data in XML format
elements
contains XML elements.
properties
id (text, r/o) : the unique identifier of the XML data
name (text) : the name of the XML data
text (text) : the text representation of the XML data
XML element‚n [inh. item] : A unit of data in XML format
elements
contains XML attributes, XML elements; contained by XML datas, XML elements.
properties
id (text, r/o) : the unique identifier of the XML element
name (text, r/o) : the name of the XML element
value (any) : the value of the XML element
XML file‚n [inh. file > disk item > item] : A file containing data in XML format
properties
contents (XML data) : the contents of the XML file; elements and properties of the XML data may be accessed as if they were elements and properties of the XML file
Here’s an example of how to use this in practice to get you started:
set theFinalValues to {}
set XMLfile to "Macintosh HD:Users:myname:Desktop:TestXML.xml"
tell application "System Events"
tell XML element "Main" of contents of XML file XMLfile
repeat with thisElement from 1 to (count of XML elements)
set dataValue to (value of XML attribute of XML element thisElement) as string
set typeText to (value of (XML elements whose name is "type") of XML element thisElement) as string
set nameText to (value of (XML elements whose name is "name") of XML element thisElement) as string
set displayname to (value of (XML elements whose name is "displayname") of XML element thisElement) as string
set download to (value of (XML elements whose name is "download") of XML element thisElement) as string
set theFinalValues to theFinalValues & {{dataValue, typeText, nameText, displayname, download}}
end repeat
end tell
end tell
repeat with thisData from 1 to count theFinalValues
if item 1 of item thisData of theFinalValues is "8086:295e" then
set typeText to item 2 of item thisData of theFinalValues
set nameText to item 3 of item thisData of theFinalValues
set displayname to item 4 of item thisData of theFinalValues
set download to item 5 of item thisData of theFinalValues
set theFinalSearchValue to {(item 1 of item thisData of theFinalValues), typeText, nameText, displayname, download}
end if
end repeat
theFinalSearchValue
Model: iMac Intel 10.5.8
Browser: Firefox 3.0.2
Operating System: Mac OS X (10.5)