PrettyPrint XStream XMLを無効にして、StyleSheetとPrologを追加する方法は?

StackOverflow https://stackoverflow.com/questions/19855158

  •  29-07-2022
  •  | 
  •  

質問

compactwriter()を使用してXML(すべてのNewlines/Carriage Returns)のPrettyPrintを削除できますが、プロログとスタイルシートを維持するにはどうすればよいですか?

現在、ライタークラスを使用して、書き込み方法を使用してPrologとStyleSheetを追加しています。

以下は、オブジェクトをシリアル化するための私の機能です。

private void serializeData(DiagData diagData){  
        fileinfo=new HashMap<String,String>();
        XStream xstream = new XStream();
        xstream.processAnnotations(DiagData.class);

        FileOutputStream fileOutputStream=null;
        Writer writer=null;
        //CompactWriter writer=null;
        xstream.registerConverter(new MapConverter());

        try {
            String path = Constants.XML_PATH+File.separator+Constants.DIRECTORY_NAME;
            File diagnosticDir = new File(path);

            String serialNumber=null;

            IDataCollector dataCollector=new DataCollector();
            serialNumber=dataCollector.getSerialNumber();

            String fileName = new SimpleDateFormat("yyyyMMddhhmmss'.xml'").format(new Date());
            if(serialNumber!=null)fileName=serialNumber+Constants.UNDERSCORE+fileName;
            fileName=Constants.PHONEHOME+Constants.UNDERSCORE+fileName;

            fileOutputStream = new FileOutputStream(path+File.separator+fileName);

            writer = new OutputStreamWriter(fileOutputStream);
            //writer = new CompactWriter(new OutputStreamWriter(fileOutputStream));

            writer.write(Constants.PROLOG);
            writer.write(Constants.STYLESHEET);
            xstream.toXML(diagData, writer);
            //xstream.marshal(diagData, writer);

        } catch (FileNotFoundException e1) {

        } catch (Exception e) {

        } finally {
            try {
                fileOutputStream.close();
                writer.close();             
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }
    }
役に立ちましたか?

解決

XStreamを見ると ドキュメンテーション, 、彼らはあなたがXMLプロログを自分で追加しなければならないことを明確に述べています:

Why does XStream not write an XML declaration?

XStream is designed to write XML snippets, so you can embed its output into 
an existing stream or string. You can write the XML declaration yourself into 
the Writer before using it to call XStream.toXML(writer).

次のコードが機能するはずです。私はあなたのコードのほとんどを削除したので、あなたはそれを元に戻さなければなりません。目的は、大まかな作業例を与えることです。

private static void serializeData(Object diagData) throws Exception {
    XStream xstream = new XStream();
    xstream.processAnnotations(DiagData.class);

    FileOutputStream fileOutputStream = null;
    Writer writer = new PrintWriter(new File(your file));
    CompactWriter compactWriter = new CompactWriter(writer);

    try {
        writer.write(your xml prolog);
        writer.write(your stylesheet);
        xstream.marshal(diagData, compactWriter);
    } catch (FileNotFoundException e1) {

    } catch (Exception e) {

    } finally {
       release resources
   }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top