Creating a plist using Java
Asked Answered
S

5

8

Is there an easy way to create a plist with Java? The result should be the same as serializing a dictionary in Objective C.

Sniperscope answered 24/7, 2010 at 10:52 Comment(1)
P
4

The PList class from code.google.com/xmlwise looks more promising to me.

Pluvial answered 24/7, 2010 at 12:16 Comment(1)
If you prefer not to use 3rd party libraries, see my answer: https://mcmap.net/q/1341784/-creating-a-plist-using-javaAcceptor
M
2

You don't need any external Java libraries. Use the following steps:

  1. Create an empty, stand-alone DOM document.

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation di = builder.getDOMImplementation();
    DocumentType dt = di.createDocumentType("plist",
      "-//Apple//DTD PLIST 1.0//EN",
      "http://www.apple.com/DTDs/PropertyList-1.0.dtd");
    Document doc = di.createDocument("", "plist", dt);
    doc.setXmlStandalone(true);
    
  2. Set plist version.

    Element root = doc.getDocumentElement();
    root.setAttribute("version", "1.0");
    
  3. Enter data.

    Element rootDict = doc.createElement("dict");
    root.appendChild(rootDict);
    Element sampleKey = doc.createElement("key");
    sampleKey.setTextContent("foo");
    rootDict.appendChild(sampleKey);
    Element sampleValue = doc.createElement("string");
    sampleValue.setTextContent("bar");
    rootDict.appendChild(sampleValue);
    
  4. Create a transformer.

    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.ENCODING, "UTF-16");
    t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, dt.getPublicId());
    t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    
  5. Write to file.

    StringWriter stringWriter = new StringWriter();
    StreamResult streamResult = new StreamResult(stringWriter);
    t.transform(domSource, streamResult);
    String xml = stringWriter.toString();
    System.out.println(xml); // Optionally output to standard output.
    OutputStream stream = new FileOutputStream("example.plist");
    Writer writer = new OutputStreamWriter(stream, "UTF-16");
    writer.write(xml);
    writer.close();
    

You can then read such a file in Objective-C as described by the Property List Programming Guide.

Miasma answered 23/7, 2012 at 19:42 Comment(0)
C
1

Here you can find a PList class for creating PList very easily.

Careerism answered 24/7, 2010 at 10:54 Comment(0)
D
1

You can use this library: http://plist.sf.net/

It will write NSObjects to files and vice versa.

Dextrin answered 18/9, 2011 at 16:12 Comment(0)
A
0

The existing answers look to complicated for simple cases. Here is a restricted shorter version:

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.FileUtils;


public class PList {

    public static String toPlist(Map<String,String> map) {
        String s = "";
        s += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        s += "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
        s += "<plist version=\"1.0\">\n";
        s += "<dict>\n";

        for(Entry<String,String> entry : map.entrySet()) {
            s += "  <key>" + entry.getKey() + "</key>\n";
            s += "    <string>" + entry.getValue() + "</string>\n";
        }

        s += "</dict>\n";
        s += "</plist>\n";
        return s;
    }

    public static void writePlistToFile(Map<String,String> map, File f) throws IOException {
        FileUtils.writeStringToFile(f, toPlist(map), "utf-8");
    }

}
Applecart answered 23/4, 2014 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.