Java 如何禁用预打印xstream xml并仍然添加样式表和prolog?

Java 如何禁用预打印xstream xml并仍然添加样式表和prolog?,java,xstream,Java,Xstream,我可以使用CompactWriter()删除xml中的预打印(所有换行符/回车符),但是如何保留我的序言和样式表呢 目前我正在使用Writer类使用write方法添加prolog和样式表 下面是我序列化对象的函数 private void serializeData(DiagData diagData){ fileinfo=new HashMap<String,String>(); XStream xstream = new XStream();

我可以使用CompactWriter()删除xml中的预打印(所有换行符/回车符),但是如何保留我的序言和样式表呢

目前我正在使用Writer类使用write方法添加prolog和样式表

下面是我序列化对象的函数

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
            }
        }
    }
private void序列化数据(DiagData DiagData){
fileinfo=newhashmap();
XStream XStream=新的XStream();
processAnnotations(DiagData.class);
FileOutputStream FileOutputStream=null;
Writer=null;
//CompactWriter=null;
registerConverter(新的MapConverter());
试一试{
String path=Constants.XML\u path+File.separator+Constants.DIRECTORY\u NAME;
文件诊断目录=新文件(路径);
字符串serialNumber=null;
IDataCollector dataCollector=新dataCollector();
serialNumber=dataCollector.getSerialNumber();
字符串文件名=新的SimpleDataFormat(“yyyyMMddhhmmss.xml”)。格式(新日期();
如果(serialNumber!=null)fileName=serialNumber+常量。下划线+文件名;
fileName=常量。PHONEHOME+常量。下划线+文件名;
fileOutputStream=新的fileOutputStream(路径+文件.分隔符+文件名);
writer=新的OutputStreamWriter(fileOutputStream);
//writer=newcompactwriter(newoutputstreamwriter(fileOutputStream));
writer.write(Constants.PROLOG);
writer.write(常量.样式表);
toXML(diagData,writer);
//xstream.marshal(diagData,writer);
}捕获(FileNotFoundException e1){
}捕获(例外e){
}最后{
试一试{
fileOutputStream.close();
writer.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
}

如果您查看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
   }
}

如果您查看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
   }
}

如果您查看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
   }
}

如果您查看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
   }
}

你能用
CompactWriter
class替换我代码中的
Writer
class吗?我的代码中已经有了它,只需要取消对Writer对象的注释和注释,您可以用
CompactWriter
类替换我代码中的
Writer
类来给我一个代码片段吗?我的代码中已经有了它,只需要取消对Writer对象的注释和注释,您可以用
CompactWriter
类替换我代码中的
Writer
类来给我一个代码片段吗?我的代码中已经有了它,只需要取消对Writer对象的注释和注释,您可以用
CompactWriter
类替换我代码中的
Writer
类来给我一个代码片段吗?我的代码中已经有了它,只需要取消对Writer对象的注释和注释