StAX - 使用 XMLStreamWriter 设置版本和编码
我使用 StAX 创建 XML 文件,然后使用 XSD 验证该文件。
我在创建 XML 文件时遇到错误:
javax.xml.stream.XMLStreamException: Underlying stream encoding 'Cp1252' and input paramter for writeStartDocument() method 'UTF-8' do not match.
at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeStartDocument(XMLStreamWriterImpl.java:1182)
这是代码片段:
XMLOutputFactory xof = XMLOutputFactory.newInstance();
try{
XMLStreamWriter xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
xtw.writeStartDocument("UTF-8","1.0");} catch(XMLStreamException e) {
e.printStackTrace();
} catch(IOException ie) {
ie.printStackTrace();
}
我在 Unix 上运行此代码。有人知道如何设置版本和编码风格吗?
I am using StAX for creating XML files and then validating the file with and XSD.
I am getting an error while creating the XML file:
javax.xml.stream.XMLStreamException: Underlying stream encoding 'Cp1252' and input paramter for writeStartDocument() method 'UTF-8' do not match.
at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeStartDocument(XMLStreamWriterImpl.java:1182)
Here is the code snippet:
XMLOutputFactory xof = XMLOutputFactory.newInstance();
try{
XMLStreamWriter xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
xtw.writeStartDocument("UTF-8","1.0");} catch(XMLStreamException e) {
e.printStackTrace();
} catch(IOException ie) {
ie.printStackTrace();
}
I am running this code on Unix. Does anybody know how to set the version and encoding style?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也会尝试将
createXMLStreamWriter()
与输出参数一起使用。[编辑]尝试过,它通过更改 createXMLStreamWriter 行来工作:
[编辑2]做了一些更复杂的测试,以记录:
I would try to use the
createXMLStreamWriter()
with an output parameter too.[EDIT] Tried, it works by changing the createXMLStreamWriter line:
[EDIT 2] Made a little more complex test, for the record:
这应该有效:
This should work:
从代码中很难确定,但如果您依赖 JDK 1.6 提供的默认 Stax 实现 (Sun sjsxp),我建议升级为使用 伍德斯托克斯。
众所周知,它比 Sjsxp 的错误更少,支持整个 Stax2 API,并且得到了积极的开发和支持(而 Sun 版本刚刚编写,错误修复的数量有限)。
但代码中的错误是这样的:
您依赖于默认平台编码(必须是 CP-1252,Windows?)。您应该始终明确指定您正在使用的编码。流编写器只是验证您没有做危险的事情,并发现可能导致文档损坏的不一致。非常聪明,这实际上表明这不是默认的 Stax 处理器。 :-)
(另一个答案也指出了一个正确的解决方法,只需传递 OutputStream 和编码即可让 XMLStreamWriter 做正确的事情)
From the code it is hard to know for sure, but if you are relying on the default Stax implementation that JDK 1.6 provides (Sun sjsxp) I would recommend upgrading to use Woodstox.
It is known to be less buggy than Sjsxp, supports the whole Stax2 API and has been actively developed and supported (whereas Sun version was just written and there has been limited number of bug fixes).
But the bug in your code is this:
you are relying on the default platform encoding (which must be CP-1252, windows?). You should always explicitly specify encoding you are using. Stream writer is just verifying that you are not doing something dangerous, and spotted inconsistence that can cause corrupt document. Pretty smart, which actually suggests that this is not the default Stax processor. :-)
(the other answer points a correct workaround, too, by just passing OutputStream and encoding to let XMLStreamWriter do the right thing)
如果使用与 Oracle JRE/JDK 捆绑在一起的默认
XMLStreamWriter
,您应该始终XMLStreamWriter
,显式设置字符编码:xmlOutputFactory .createXMLStreamWriter(in,encoding)
xmlStreamWriter.writeStartDocument(encoding, version)
。 writer 不够聪明,无法记住创建 writer 时的编码集。但是,它会检查这些编码是否相同。请参阅下面的代码。这样,您的文件编码和 XML 声明始终保持同步。尽管在 XML 声明中指定编码是可选的,但 XML 最佳实践是始终指定它。
这是来自 Oracle (Sun) 实现 (Sjsxp) 的代码:
If using the default
XMLStreamWriter
bundled with the Oracle JRE/JDK you should alwaysXMLStreamWriter
, explicitly setting the character encoding:xmlOutputFactory.createXMLStreamWriter(in, encoding)
xmlStreamWriter.writeStartDocument(encoding, version)
. The writer is not smart enough remembering the encoding set when the writer was created. However, it checks if these encodings are the same. See code below.This way, your file encoding and XML declaration are always in sync. Although specifying the encoding in the XML declaration is optional, XML best practice is to always specify it.
This is the code from the Oracle (Sun) implementation (Sjsxp):