Re: Getting started with XML?
Re: Getting started with XML?
- Subject: Re: Getting started with XML?
- From: Christopher Nebel <email@hidden>
- Date: Thu, 7 Dec 2006 14:10:34 -0800
On Dec 7, 2006, at 11:57 AM, Michelle Steiner wrote:
property XMLfile : missing value
if XMLfile is missing value then set the XMLfile to choose file
tell application "System Events"
set foo to contents of XMLfile
end tell
This returns an alias to the file. The dictionary says:
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
So how do I get the contents of the file? I tried
XML data of foo
but that returns an error
First, you didn't get the file contents of anything with that. You
got the same alias object back as you passed in, because the
"contents" of any primitive AppleScript value other than a reference,
such as an alias, is that same value.
Second, System Events isn't magically smart about file contents -- you
have to tell it what kind of file you think you're dealing with, which
means building a System Events object specifier. In this case:
-- assuming that "XMLfile" has been set to an alias...
tell application "System Events"
set foo to contents of XML file (path of f) -- "'f as text' would
have worked, too, as would 'POSIX path of f'."
end tell
The explicit "path of" is necessary because System Events does not
recognize an alias a selector data for a file.
Once you're done there, "foo" will be an "XML data" object specifier.
To get parts of it, direct your eyes upward in the dictionary from
"XML file" to "XML data" and observe that an "XML data" contains "XML
elements". You can also get the text representation by saying "text
of", but for an element that's mostly only useful for debugging.
Don't forget that if the XML file you're dealing with is a plist, then
you ought to be using the Property List Suite instead. It's easier to
work with, and there's no guarantee that a plist is XML.
--Chris Nebel
AppleScript Engineering
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users
This email sent to email@hidden