How to parse .plist file in Java?
Asked Answered
B

3

7

I am trying to parse a .plist file in Java but not understanding how. I used a DOM parser but it gives an error and is not able to read .plist file.

This is my plist file:

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">
<plist version="1.0">
<dict>
<key>All Samples</key>
<array>
    <dict>
        <key>Message</key>
        <string>1) UIATarget </string>
        <key>Timestamp</key>
        <date>2011-07-06T19:40:09Z</date>
        <key>Type</key>
        <integer>0</integer>
    </dict>

This my main function:

 public static void main(String[] args) throws XMLStreamException, IOException {
    InputStream in = new FileInputStream("File.plist");
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader parser = factory.createXMLEventReader(in);

    assert parser.nextEvent().isStartDocument();

    XMLEvent event = parser.nextTag();
    //System.out.println(event.getClass());
    assert event.isStartElement();
    final String name1 = event.asStartElement().getName().getLocalPart();

    if (name1.equals("dict")) {
        while ((event = parser.nextTag()).isStartElement()) {
            final String name2 = event.asStartElement().getName().getLocalPart();

            if (name2.equals("key")) {
                String key = parser.getElementText();
                System.out.println("key: " + key);

            } else if (name2.equals("String")) {
                String number = parser.getElementText();
                System.out.println("date: " + number);

            } else if (name2.equals("date")) {
                String str = parser.getElementText();
                System.out.println("date: " + str);
            }
        }
    }

    assert parser.nextEvent().isEndDocument();
}
Ber answered 20/9, 2011 at 11:10 Comment(11)
Are you sure that you have the complete file? It looks like the closing tag is missing.Gloria
closing tag is same after dict file closes with </array> then </plist> tagBer
@Mark actually, 3 closing tags are missing: </array>, </dict> and </plist>. :) - Additionally, the starting <? is missing.Cosine
What error does the DOM parser give? How did you try to read the file (elaborate, provide some sample code)?Cosine
@Cosine yes u r ryt , while copying tag got missed, i am finding solution to parse it in javaBer
@Cosine I am having error as : expected start or end tag at com.sun.xml.internal.stream.XMLEventReaderImpl.nextTag(XMLEventReaderImpl.java:237)Ber
@TheEliteGentleman I am new user didn't know much about posting , if u have solution u can help meBer
expected start or end tag at ... That indicates your file is not well-formed. If what you posted is the complete file content, I don't have any more questions - If not, please post the complete file you tested with.Cosine
@Cosine This is the complete file i am testing , only tags are missing as mentioned in above comments, </array>, </dict>, </plist> tagsBer
How can this be a duplicate of a deleted question?Bromley
@Roflcoptr Gah! I didn't realize the proposed dup was deleted. Re-opened. Going to figure out how that happened, as I wouldn't have closed it otherwise. Something should not have let me do that, but did.Lavender
S
6

If I were you I'd use the PList class from code.google.com/xmlwise. It's specifically designed for dealing with .plist files.

Siple answered 20/9, 2011 at 11:21 Comment(1)
Xmlwise is discontinued and has a major bug regarding thread safety (uses unsynchronized SimpleDateFormat instance), so it is no longer a good choice.Sherry
T
3

You will want to look at Apache Commons Configuration at http://commons.apache.org/proper/commons-configuration/, which offers a pList parser. Here's a snippet example:

        XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration();
        // load plist from classoath
        URL url = this.getClass().getClassLoader().getResource(systemConfigFile);
        plist.setFileName(url.getFile());
        plist.load();
        Iterator<String> keys = plist.getKeys();
        while (keys.hasNext()) {
            // do someting with the value
            plist.getString(keys.next());
        }
Towhead answered 25/2, 2014 at 8:10 Comment(2)
XMLPropertyListConfiguration has bug tickets pending for many years: issues.apache.org/jira/browse/CONFIGURATION-427 Seems a bad choice for plist generation. It works well to parse plist files though. I eventually chose to use a template system (Velocity) to generate plist files.Doig
yes, true - I've actually never used it for anything else but reading anyway. Nowadays I prefer using YAML anyway.Towhead
F
2

There is yet another project on github which is directly accessible for Maven-based projects: https://github.com/3breadt/dd-plist

Fitter answered 27/6, 2021 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.