XML/plist parsing (Was: 'info for' braindead...)
XML/plist parsing (Was: 'info for' braindead...)
- Subject: XML/plist parsing (Was: 'info for' braindead...)
- From: Joseph Weaks <email@hidden>
- Date: Thu, 6 May 2004 15:01:11 -0500
On May 6, 2004, at 8:42 AM, Gnarlodious wrote:
Entity Joseph Weaks spoke thus:
Any of you care to share your brilliantly economic handlers you've
created to parse XML?
Post an example of the key you want to read.
Well, as for plist files, let's assume the following sample. To keep it
simple, it has no nested arrays, <data>, or <dict>'s. However, there is
often a nested <dict> in arrays for instance, so it'd be nice if the
presence of one didn't break the handler. Ideally I'd like to see a
good way to implement a handler something like the following:
set plistString to "
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"
http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>myString</key>
<string>I am a string</string>
<key>myReal</key>
<real>1</real>
<key>myInteger</key>
<integer>2</integer>
<key>trueBoolean</key>
<true/>
<key>falseBoolean</key>
<false/>
<key>myList</key>
<array>
<string>itemA</string>
<string>itemB</string>
</array>
</dict>
</plist>"
on getKeyValue(plistString, theKey)
-- clever script here
end getKey
set theResult to getKeyValue(theString, "myString")
-- Returns "I am a string"
set theResult to getKeyValue(theString, "myReal")
-- Returns 1 as real
set theResult to getKeyValue(theString, "myInteger")
-- Returns 2 as integer
set theResult to getKeyValue(theString, "trueBoolean")
-- Returns true as boolean
set theResult to getKeyValue(theString, "falseBoolean")
-- Returns false as boolean
set theResult to getKey(theString, "myList")
-- Returns {"itemA","itemB"} as list
Now, how about another handler for another application that reads a
file that contains something like this:
<name>Joe Weaks</name>
<state>Texas</state>
<age>33</age>
<hobbies>applescript, astronomy, scuba diving</hobbies>
Perhaps something with offset, as Walter uses?
set theKey to "hobbies"
set startOffset to (offset of ("<" & theKey & ">") in theString) +
(length of theKey) +2
set endOffset to (offset of ("</" & theKey & ">") in theString)
set v to text startOffset thru endOffset of theString
Of course, then there's a whole 'nother set of handlers for each XML
type when the file breaks the 32k limit.
Cheers,
Joe
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.