XML to List

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.

Is this possible?

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

Thanks. I tried this and i get:

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.

Late Night Software do a Free XML tool. It may help (not used it myself) but.

http://www.latenightsw.com/freeware/XMLTools2/

Thanks. I have tried all OSAXes and System Events, but i still cant figure out how to get result something like this:

{{“TITLE1”, “DESCR1”, "DATE1}, {“TITLE2”, “DESCR2”, “DATE2”}}

<?xml version="1.0" standalone='yes'?>

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
   <channel>
      <title>A</title>
      <link>A</link>
      <description>A</description>
      <language>en-us</language>
      <pubDate>Fri, 11 Feb 2011 09:25:28 +0000</pubDate>
      <lastBuildDate>Fri, 11 Feb 2011 09:25:28 +0000</lastBuildDate>
      <ttl>15</ttl>
      <atom:link rel="edit" href="LINK"/>

      <item>
         <title>TITLE1</title>
         <description>DESCR1</description>
         <pubDate>DATE1</pubDate>
       </item>

      <item>
         <title>TITLE2</title>
         <description>DESCR2</description>
         <pubDate>DATE2</pubDate>
       </item>

   </channel>
</rss>

Don’t have time to work out the exact coding for you, but two thoughts:

  1. The late night software tool posted above is really good, especially if you want to write updates back out to an XML file.

  2. 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)