I receive student rosters in the form of xml files from a university student information system. They recently switched to an xml output rather than tab-delimited export format. I’d like to be able to read this xml straight into an AS Studio app table so I can use it further.
I figured out how to have a plain AppleScript parse the file using System Events, so I figured it would be easy to use this code in AS Studio. I used an existing document-based AS Studio app that reads in tab delimited rosters, again thinking it would be simple to adapt that to this new xml data format. Well, that was three days ago, and I have to admit that this simple thing has become an obession–you know there is nothing worse than something that should be simple!
I’ve scoured the forums under ‘xml,’ ‘read xml,’ ‘parse xml,’ ‘xml to tables,’ and ‘system events.’ Nothing seems to solve this thorny problem. I’m also not sure if NSArrays would be of any help–I’m not versed in call methods, etc. I’d like to avoid 3rd party solutions since these inevitably go away, and besides, I like the home-grown solutions, and ‘System Events’ seems like a nutural!
Here is the code for an ‘Import’ button, with comments I’ve inserted. It is meant to be readable, so I hope the problem is clear. (You can tell that I have plagarized all sorts of things from this forum, and the doc-based code is from an AS Studio example. I am sure the basics are familiar to folks in this forum!)
A sample xml file:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> bondicja Bondicova Jenny bond@.edu
on clicked theObject
set theWindow to window 1 of theObject
set theDataSource to data source of table view "roster" of scroll view "roster" of theWindow
if name of theObject is equal to "import" then
--Begin by getting the roster file
choose file with prompt ("Select an XML file with your roster") without invisibles
set xmlFile to result as Unicode text
tell application "System Events"
tell contents of XML file xmlFile
set theXMLDB to the name of XML element 1 --this returns the main element, which should have title data of the xml file
set theRecordCount to the count of XML elements of XML element theXMLDB --this returns the next level of the XML, all of the 'records'
--the parsing can begin here
repeat with theRecord from 1 to theRecordCount
--I would think the XML file could be read straight into the table at this point...
--but it appears to have a scope problem.
--[b]When run, the following line produces an error...
-->System Events got an error: Can't make every row of contents of XML file "~:Desktop:XML stuff:RosterXML.xml" into type reference. (-1700)[/b] set theRow to make new data row at the end of data rows of theDataSource
-- [b]The following line prevents a compile! I assume for the same reason as above, but then how do I get this into a table?[/b]
-->set contents of data cell "studentID" of theRow to (the value of (XML element 1 of XML element theRecord of XML element theXMLDB))
-->further data cells would go here...
end repeat
end tell
end tell
end if --END IMPORT
end clicked