[Pythonmac-SIG] convert binary plist to xml string

Nicholas Riley njriley at uiuc.edu
Sat May 7 19:51:50 CEST 2005


On May 7, 2005, at 12:13 PM, Florian Munz wrote:

> since the format of plist files changed to binary by default on
> Tiger my little program, which works on xml plists doesn't work
> anymore.
>
> Is there a way to convert a binary plist to xml with Python or PyObjC?
>
> I know this on the command line:
>
> plutil -convert xml1 Bookmarks.plist
>
> but I'm searching a way to do this directly.

In [1]: from Foundation import *

In [2]: import os

In [3]: plist, format, error = NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_(NSData.dataWithContentsOfMappedFile_(os.path.expanduser('~/Library/Preferences/com.apple.keychainsync.plist')), NSPropertyListImmutable, 0)

In [4]: plist
Out[4]: {KeychainSyncList = (); }

In [5]: data, error =  
NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_(plist, NSPropertyListXMLFormat_v1_0)

In [6]: data.writeToFile_atomically_('foo.plist', False)
Out[6]: 1

In [7]: print file('foo.plist').read()
<?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>KeychainSyncList</key>
         <array/>
</dict>
</plist>

-- 
Nicholas Riley <njriley at uiuc.edu>     | 4111 Siebel Center
Department of Computer Science        | 201 N. Goodwin Ave.
and Medical Scholars Program          | Urbana, IL 61801-2302
Univ. of Illinois at Urbana-Champaign | +1 217 244 2274




More information about the Pythonmac-SIG mailing list