我正在使用 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 上运行这段代码。有人知道如何设置版本和编码样式吗?