إذا نظرت إلى 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
}
}