PrettyPrint Xstream XML을 비활성화하고 여전히 스타일 시트와 프롤로그를 추가하는 방법?

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

  •  29-07-2022
  •  | 
  •  

문제

CompactWriter ()를 사용하여 XML (모든 Newlines/Carriage Returns)에서 PrettyPrint를 제거 할 수 있지만 프롤로그와 스타일 시트를 유지하는 방법은 무엇입니까?

현재 Writer Class를 사용하여 Write Method를 사용하여 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 Prolog를 직접 추가해야한다고 분명히 말합니다.

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