7
votes

Créer une plique à l'aide de Java

Y a-t-il un moyen facile de créer une plis avec Java? Le résultat devrait être le même que de sérialiser un dictionnaire dans l'objectif c.


0 commentaires

5 Réponses :


1
votes

ici Vous pouvez trouver une classe de plist pour la création de plist très facilement.


0 commentaires

4
votes

Le PLIST Classe de code.google.com/xmllise semble plus prometteur pour moi.


1 commentaires

Si vous préférez ne pas utiliser de bibliothèques tiers, voir ma réponse: Stackoverflow.com/a/11619384/974531



1
votes

Vous pouvez utiliser cette bibliothèque: http://plist.sf.net/

Il écrira NsObjects aux fichiers et inversement.


0 commentaires

2
votes

Vous n'avez pas besoin de bibliothèques de Java externes. Utilisez les étapes suivantes:

  1. Créez un document Dom vide et autonome. P>

    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();
    
  2. Définir la version du plis. p>

    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");
    
  3. entrez les données. p>

    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. Créer un transformateur. P>

    Element root = doc.getDocumentElement();
    root.setAttribute("version", "1.0");
    
  5. écrire dans le fichier. p>

    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);
    


0 commentaires

0
votes

Les réponses existantes regardent à compliquer pour des cas simples. Voici une version plus courte restreinte:

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");
    }

}


0 commentaires