【发布时间】:2010-07-24 10:52:30
【问题描述】:
有没有用 Java 创建 plist 的简单方法?结果应该与在 Objective C 中序列化字典相同。
【问题讨论】:
标签: java objective-c serialization plist
有没有用 Java 创建 plist 的简单方法?结果应该与在 Objective C 中序列化字典相同。
【问题讨论】:
标签: java objective-c serialization plist
code.google.com/xmlwise 中的 PList 类对我来说看起来更有前途。
【讨论】:
您不需要任何外部 Java 库。使用以下步骤:
创建一个空的、独立的 DOM 文档。
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);
设置 plist 版本。
Element root = doc.getDocumentElement();
root.setAttribute("version", "1.0");
输入数据。
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);
创建一个转换器。
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");
写入文件。
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();
然后您可以按照Property List Programming Guide 的描述在 Objective-C 中读取这样的文件。
【讨论】:
Here你可以很容易地找到一个PList类来创建PList。
【讨论】:
你可以使用这个库:http://plist.sf.net/
它将 NSObjects 写入文件,反之亦然。
【讨论】:
对于简单的情况,现有的答案看起来很复杂。这是一个受限制的较短版本:
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");
}
}
【讨论】: