plist string into NSDictionary

Hi! I’m looking for a way to parse a plist string. For example:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<plist version="0.9">

key1
value1
key2
value2

"

Let’s say, we want to get “value1”. I think NSDictionary might be a solution, but I only found examples in Obj-C. How can I do it in ASOC?

Hi,

if the plist is on disk, you can directly read the file into a NSDictionary (the top dict tag)


set thePlist to current application's NSDictionary's dictionaryWithContentsOfFile_("/path/to/file.plist")
set theValue to thePlist's objectForKey_("key1")

Thanks. No, it’s not a file, but the result of a shell script. So i have a string called myXmlData for example.

try this, the snippet expects the xml text in the variable xmlText


set theData to current application's NSString's stringWithString_(xmlText)'s dataUsingEncoding_(4)
set theDictionary to current application's NSPropertyListSerialization's propertyListFromData_mutabilityOption_format_errorDescription_(theData, 0, missing value, missing value)
log theDictionary's objectForKey_("key1")


Once again: thanks a lot, Stefan!! This saved my day (better: night)!

This propertyListFromData_mutabilityOption_format_errorDescription_ is a nice code monster…