确保在使用 Java 在 Windows 上生成 XML 时使用 Unix 样式的行结尾 [英] Ensure Unix-style line endings used when generating XML on Windows with Java

查看:22
本文介绍了确保在使用 Java 在 Windows 上生成 XML 时使用 Unix 样式的行结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将 XML 文档输出到 Java 文件中,如下所示:

I am currently outputting an XML document to a file in Java as follows:

final Document xmldoc = toXMLDocument(docTheory);

// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(xmldoc);
StreamResult result = new StreamResult(file);

transformer.transform(source, result);

然而,当在 Windows 上运行时,这会产生带有 Windows 风格行尾的输出.无论操作系统如何,我都希望生成 Unix 风格的行尾.我该怎么做?

However, when run on Windows, this produces output with Windows-style line-endings. I would like to have Unix-style line endings produced regardless of OS. How I can do this?

推荐答案

如果你使用 LSSerializer 而不是 Transformer,你可以使用 LSSerializer.setNewLine 控制换行.

If you use LSSerializer rather than Transformer, you can use LSSerializer.setNewLine to control newlines.

这篇关于确保在使用 Java 在 Windows 上生成 XML 时使用 Unix 样式的行结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆