Is there an easy way to create a plist with Java? The result should be the same as serializing a dictionary in Objective C.
The PList class from code.google.com/xmlwise looks more promising to me.
You don't need any external Java libraries. Use the following steps:
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);
Set plist version.
Element root = doc.getDocumentElement();
root.setAttribute("version", "1.0");
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);
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With