微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Windows上的Marshaller在文件末尾添加新行

我有一个项目使用JAXB编组的XML文件来比较不同环境的configuration状态。 我注意到,在Unix版本下,在Windows下的JAXB编码器的实现中必然存在一些差异。 当我比较在不同平台上创build的两个文件时,我的比较工具总是在文件末尾标记一个差异。 在Windows上创build的文件文件末尾有一个新行(CR和LF),而Unix版本没有它。

请注意,问题不在于两个平台之间的新行字符的差异 ! Windows编组器在文件末尾有效地添加一个“新行”,而Unix编组器在根标记的closures“>”之后停止。

是否有任何参数,我可以传递给编组,以防止这个额外的行,或者我必须显式删除它在Windows上编组后,以便我的比较工具不标记差异?

尝试通过SSL连接到服务器的SSLHandshakeException

POI – 在Excel中打开时无法写入文件

Java 1.8.65 javac缺失

使用java运行shell脚本时出现问题

java.io.IOException的可能原因:CreateProcess error = 5

编组代码如下所示:

public void marshal(final Object rootObject,final OutputStream outputStream) throws JAXBException,TransformerException { Preconditions.checkArgument(rootObject != null,"rootObject must not be null"); Preconditions.checkArgument(outputStream != null,"outputStream must not be null"); final JAXBContext ctx = JAXBContext.newInstance(rootObject.getClass()); final Document document = getFactories().newDocument(); document.setXmlStandalone(true); final Marshaller marshaller = ctx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); marshaller.setSchema(schema); marshaller.marshal(rootObject,document); createTransformer().transform(new DOMSource(document),new StreamResult(outputStream)); } public static Transformer createTransformer() { final Transformer transformer = getFactories().newTransformer(); transformer.setoutputProperty(OutputKeys.INDENT,"yes"); transformer.setoutputProperty(OutputKeys.STANDALONE,"yes"); transformer.setoutputProperty(OutputKeys.ENCODING,JAXBDefaults.OUTPUT_CHARSET.name()); transformer.setoutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS,CDATA_XML_ELEMENTS); transformer.setoutputProperty("{http://xml.apache.org/xslt}indent-amount",IDENT_LENGTH); return transformer; } private static class JAXBFactories { private DocumentBuilderFactory documentBuilderFactory; public DocumentBuilderFactory getDocumentBuilderFactory() { if (documentBuilderFactory == null) { documentBuilderFactory = DocumentBuilderFactory.newInstance(); } return documentBuilderFactory; } private DocumentBuilder documentBuilder; public DocumentBuilder getDocumentBuilder() { if (documentBuilder == null) { try { documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); } catch (final ParserConfigurationException ex) { throw new RuntimeException("Failed to create DocumentBuilder",ex); } } return documentBuilder; } public Document newDocument() { return getDocumentBuilder().newDocument(); } private TransformerFactory transformerFactory; public TransformerFactory getTransformerFactory() { if (transformerFactory == null) { transformerFactory = TransformerFactory.newInstance(); } return transformerFactory; } public Transformer newTransformer() { try { return getTransformerFactory().newTransformer(); } catch (final TransformerConfigurationException ex) { throw new RuntimeException("Failed to create Transformer",ex); } } } private static class FactoriesHolder { static final JAXBFactories FACTORIES = new JAXBFactories(); } private static JAXBFactories getFactories() { return FactoriesHolder.FACTORIES; }

如何杀死从Java内部产生的ant程序

原生Java文档分析器和转换器基于库/ Linux的文档转换器

如何同时运行java 6和java 7

在hdfs的文件系统中复制错误

如何在Windows和Java下与USB设备进行通信?

没有理由(或期望)漂亮的打印XML将产生完全相同的结果从两个不同的系统。 但是,看起来很可能,如果关闭了漂亮的打印(并且让IDE /编辑器执行此操作),则可能会发现输出是相同的。

漂亮的打印XML是原来的添加布局的转换。 它不再是真正的 XML。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐