Anyone have any luck using System Event's xml commands? I've tried
with no luck.
I'm going to try to write my own routines to manage xml data. Any
tips people have would be appreciated.
Yeah, that's probably not a good idea. XML was designed to be easy
to parse, but there are still a number of subtleties that are hard to
get right, especially in a language like AppleScript. Try to use a
tool that's designed for the job, such as System Events. For
example, say you've got the following (abbreviated) XML document,
inspired by Graham's message:
<book>
<chapter title="In the Beginning">blah blah blah</chapter>
<chapter title="Later">yadda yadda yadda</chapter>
<appendix title="Meanwhile, Back at the Ranch">...</appendix>
</book>
Here's how to pick out various bits:
tell application "System Events"
set f to XML file "/path/to/file.xml" -- HFS paths work, too.
-- get the root element.
set root to XML element 1 of f
get properties of root
--> {class:XML element, name:"book", value:missing value}
-- get the content of every section.
get value of every XML element of root
--> {"blah blah blah", "yadda yadda yadda", "..."}
-- get the title of every chapter.
get value of XML attribute "title" of (every XML element whose name
is "chapter") of root
--> {"In the Beginning", "Later"}
end tell
The key bit here is that elements and attributes are treated as
objects, not primitive values. If you want to find out the text
value, you have to ask for the appropriate property.
Depending on what you're doing, AppleScript may not be the proper
solution for you. For simple transforms and extractions, I'm rather
fond of XSLT these days. (Some people think I'm mad, but there you
are.) Something that understands XPath (such as XSLT) will probably
simplify your life dramatically. For example, the last bit in the
above example would be expressed in XPath as "/book/chapter/@title".