iteye_3692 2010-06-30 15:39
浏览 398
已采纳

在使用StAX中,XMLStreamWriter.writeStartDocument()遇到的一个问题,求教!

最近在使用StAX的时候,看了很多资料,
XMLStreamWriter.writeStartDocument()有很多重载:
void writeStartDocument()
void writeStartDocument(String version)
void writeStartDocument(String encoding, String version)

在使用最后一个的时候就有问题了,
如果指定encoding,就可能会报错:
Exception in thread "main" javax.xml.stream.XMLStreamException: Underlying stream encoding 'GBK' and input paramter for writeStartDocument() method 'ISO-8859-1' do not match.

查找之后发现这个是与底层流编码不匹配的问题造成的。

StAX是一套接口,我现在所用的是JDK1.6_20里面的实现。
之前在工作里用的 bea 实现的就没有遇到过该问题。
请问有人遇到过这种问题么?
如果使用JDK1.6应该如何处理底层流编码呢?
附上源代码
[code="java"]
package com.yy.xml.stax.test;

import java.io.OutputStream;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public class StAXTest {
public static void main(String[] args) throws XMLStreamException {
String ENCODING = "ISO-8859-1";
// Create an output factory
XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
// Create an XML stream writer
XMLStreamWriter xmlw = xmlof.createXMLStreamWriter(System.out);
// Write XML prologue
//xmlw.writeStartDocument();
xmlw.writeStartDocument(ENCODING, "1.0");
}
}
[/code]

  • 写回答

1条回答 默认 最新

  • myali88 2010-06-30 16:57
    关注

    [quote]
    Write the XML Declaration. Note that the encoding parameter does not set the actual encoding of the underlying output. [b]That must be set when the instance of the XMLStreamWriter is created using the XMLOutputFactory[/b]
    [/quote]
    writeStartDocument方法里说的明白,你采用下面的方式试试:
    [code="java"]
    String ENCODING = "ISO-8859-1";

    // Create an output factory

    XMLOutputFactory xmlof = XMLOutputFactory.newInstance();

    // Create an XML stream writer

    XMLStreamWriter xmlw = xmlof.createXMLStreamWriter(System.out , ENCODING);

    // Write XML prologue

    //xmlw.writeStartDocument();

    xmlw.writeStartDocument(ENCODING, "1.0");

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制