Re: Accessing XML property list values with Applescript
Re: Accessing XML property list values with Applescript
- Subject: Re: Accessing XML property list values with Applescript
- From: Axel Luttgens <email@hidden>
- Date: Sun, 05 Jan 2003 16:55:42 +0100
Jerry Podhajsky wrote:
>
I'm using Late Night Software's XML Tools to parse an XML file that contains
>
property lists that look something like:
>
>
<dict>
>
<key>Item 1</key>
>
<string>Moe</string>
>
<key>Item 2</key>
>
<string>Larry</string>
>
<key>Item 3</key>
>
<string>Curley</string>
>
<key>Number of Stooges</key>
>
<integer>3</integer>
>
</dict>
>
>
I'm able to recursively loop through the XML file and get key names and
>
values.
>
I suppose you meant "recursively loop through the record returned by the
'parse XML' command" and will thus assume you started with something
like this:
-- Just using a text constant here
set xmlSource to "
<dict>
<key>Item 1</key>
<string>Moe</string>
<key>Item 2</key>
<string>Larry</string>
<key>Item 3</key>
<string>Curley</string>
<key>Number of Stooges</key>
<integer>3</integer>
</dict>
"
-- Parse data
set xmlDoc to parse XML xmlSource
>
However, I don't know how to say "get the value for Item 3 in this
>
particular <dict> construction".
>
Unless I'm missing something, the "parse XML" command just returns a
genuine AppleScript record whose data has to be accessed through
standard record access techniques.
So, one has to go through the hard way and, after having looked at the
kind of record returned for a given type of XML document[1], to write a
more or less procedural way to access its data.
Put in another way, there should generally speaking be no reference
forms (such as "item i of...") at hand.
>
>
Anyone got a code sample that will demonstrate how to pick out a value in a
>
property list based on the key? I'm sure it's simple, I just can't find any
>
reference on it anywhere.
>
You surely already have guessed it; something like this should do the job:
-- Very basic: no error checking, case insensitive...
on readDictKey(dictDoc, keyName)
set valueExpected to false
repeat with elem in dictDoc's XML contents
if valueExpected then
return {first item of elem's XML contents, elem's XML tag}
else if elem's XML tag is "key" then
if first item of elem's XML contents is keyName then
set valueExpected to true
end if
end if
end repeat
return false
end readDictKey
readDictKey(xmlDoc, "number of stooges")
--> {"3", "integer"}
readDictKey(xmlDoc, "item 7"}
--> false
>
>
>
Thanks in advance. Happy Holidays!
>
>
--
>
Jerry Podhajsky
>
HTH,
Axel
[1] With your example, the returned record is:
{
class:XML document,
XML tag:"dict",
XML contents:{
{
class:XML element,
XML tag:"key",
XML contents:{
"Item 1"
}
},
{
class:XML element,
XML tag:"string",
XML contents:{
"Moe"
}
},
{
class:XML element,
XML tag:"key",
XML contents:{
"Item 2"
}
},
{
class:XML element,
XML tag:"string",
XML contents:{
"Larry"
}
},
{
class:XML element,
XML tag:"key",
XML contents:{
"Item 3"
}
},
{
class:XML element,
XML tag:"string",
XML contents:{
"Curley"
}
},
{
class:XML element,
XML tag:"key",
XML contents:{
"Number of Stooges"
}
},
{
class:XML element,
XML tag:"integer",
XML contents:{
"3"
}
}
}
}
_______________________________________________
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.