使用Java创建plist有没有简单的方法?结果应该与序列化Objective C中的字典相同。使用Java创建plist
6
A
回答
4
PList职业从code.google.com/xmlwise看起来更有希望给我。
1
Here您可以非常容易地找到用于创建PList的PList类。
1
你可以使用这个库:http://plist.sf.net/
将会写入NSObjects的文件,反之亦然。
2
您不需要任何外部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这样的文件。
0
对于简单的情况,现有的答案看起来很复杂。这里是一个限制较短的版本:
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");
}
}
相关问题
- 1. Swift - 使用创建的plist
- 2. 创建的Plist
- 3. 在Java中创建二进制plist?
- 4. 使用Automator/AppleScript/Perl创建Plist
- 5. 创建应用程序plist
- 6. Xcode 4创建.plist
- 7. 从plist创建NSArray
- 8. 创建plist文件
- 9. 使用Java创建
- 10. 使用Java创建
- 11. 使用Java创建
- 12. 使用Java创建
- 13. 创建使用Java
- 14. 使用Java创建
- 15. 从plist数组创建NSArray
- 16. 创建一个plist文件
- 17. NSSring从plist创建NSArray
- 18. 创建从一个plist中
- 19. 创建一个plist iPhone SDK
- 20. 使用平铺应用程序来创建Cocos2d .plist
- 21. 错误使用Java创建
- 22. 使用Java创建报告
- 23. 使用Java创建excel
- 24. 使用java创建房屋
- 25. 使用java创建xml
- 26. 不能使用Java创建
- 27. 使用java创建PDF
- 28. 使用Java创建Acroynm
- 29. 使用Cardme创建VCard Java
- 30. 创建.ACCDB使用Java
如果你不想使用第三方库,请参阅我的答案:http://stackoverflow.com/a/11619384/974531 – 2013-04-04 08:26:28