Apple Plist Converted to Java Data Structures
I have a program that is going to be receiving info displayed in it from a .plist on my server. However, I am going to be making this for PC, and I'm using Java to do so. Plist files aren't conventional XML, so common XML parsing libraries don't work. I can't locate any plist specific parsers, so I am lost on how to continue. The way I see it my options are:
a) Locate such a parser for the plist file
b) Make a workaround using a regular XML parser
c) 开发者_运维知识库Change the file the program gets its data from (undesirable)
Has anyone else had any experience with plists in languages other than Objective-C?
As @Jochen Bedersdorfer notes, .plist
files are XML with a well-formed DTD. Use plutil
to convert them from binary to text form.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Addendum: In a comment, you asked
Isn't it text form when I get it?
Not necessarily. Here's some history on the subject.
To remove any confusion:
- A
plist
file can be either in XML or in binary form. - The binary plist is of course not XML.
- The XML plist is of course XML. In fact it's a valid XML... try
xmllint
against an XML plist. - Yes it has slightly unconventional structure. As you say, it's like
<dt>title</dt><dd>description</dd>
. - You can convert a binary plist to an XML plist using
plutil
on OS X. Convert it to XML before sending it over the network. - With a regular XML parser you can get
key
andstring
. Make a hash containing these pairs. Then you get the string based on the key.
dd-plist might be helpful.
Serialize native java data structures to property list objects
Deserialize from property list objects to native java data structures
You can also directly convert the contained NSObject objects into native Java objects with the NSObject.toJavaObject() method. Using this method you can avoid working with NSObject instances altogether.
<dependency>
<groupId>com.googlecode.plist</groupId>
<artifactId>dd-plist</artifactId>
<version>1.26</version>
</dependency>
精彩评论