Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse .plist file in Java?

Tags:

java

xml

plist

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();
}
like image 448
jatin Avatar asked Sep 20 '11 11:09

jatin


People also ask

How do I parse a plist file?

To write out and to parse a plist file, use the dump() and load() functions. To work with plist data in bytes objects, use dumps() and loads() . Values can be strings, integers, floats, booleans, tuples, lists, dictionaries (but only with string keys), bytes , bytearray or datetime.

How do I read Apple plist files?

plist - The primary property list for Mac OS X applications, located in the /​Contents/​ directory of an . APP bundle. To view this file, right-click an application file, select "Show Package Contents," and open the Contents folder.

What is plist in Java?

This library enables your Java application to handle property lists of various formats. It is licensed under the terms of the MIT license. Property lists are files used to store user settings and serialized objects.

What is Apple plist?

Apple Property List (PLIST) files are used to store serialized objects. They are mostly used in macOS, iOS, NeXTSTEP, and GNUstep programming frameworks.


1 Answers

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

like image 57
Sean Patrick Floyd Avatar answered Sep 17 '22 15:09

Sean Patrick Floyd