zxlhjr
2009-02-24 18:12
浏览 924
已采纳

怎样修改<?xml version="1.0" encoding="gb2312"?>

我先读取了xml配置文件再在里面修改了一些节点值再保存xml文档,就出现如下问题,
第一个是<?xml version="1.0" encoding="gb2312"?>中的encoding="utf-8"了
第二个是当我再次读这个xml文档时就报错了如下所示:
   org.dom4j.DocumentException: Error on line 1 of document file:///C:/Documents and Settings/Administrator/jbproject/xml/src/jieMian/my_config.xml : 字符转换错误:“Unconvertible UTF-8 character beginning with 0xbc”(行号可能太小)。 Nested exception: 字符转换错误:“Unconvertible UTF-8 character beginning with 0xbc”(行号可能太小)。

at org.dom4j.io.SAXReader.read(SAXReader.java:482)

at org.dom4j.io.SAXReader.read(SAXReader.java:264)

at jieMian.xmlManager.getXmlConfig(xmlManager.java:88)

at jieMian.XmlManagerDialog.init(XmlManagerDialog.java:102)

at jieMian.XmlManagerDialog.<init>(XmlManagerDialog.java:36)

at jieMian.XmlManagerDialog.<init>(XmlManagerDialog.java:44)

at jieMian.XmlManagerDialog_butn2_actionAdapter$1.run(XmlManagerDialog.java:183)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

Nested exception:

org.xml.sax.SAXParseException: 字符转换错误:“Unconvertible UTF-8 character beginning with 0xbc”(行号可能太小)。

at org.apache.crimson.parser.InputEntity.fatal(InputEntity.java:1100)

at org.apache.crimson.parser.InputEntity.fillbuf(InputEntity.java:1072)

at org.apache.crimson.parser.InputEntity.isXmlDeclOrTextDeclPrefix(InputEntity.java:914)

at org.apache.crimson.parser.Parser2.maybeXmlDecl(Parser2.java:1150)

at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:620)

at org.apache.crimson.parser.Parser2.parse(Parser2.java:333)

at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448)

at org.dom4j.io.SAXReader.read(SAXReader.java:465)

at org.dom4j.io.SAXReader.read(SAXReader.java:264)

at jieMian.xmlManager.getXmlConfig(xmlManager.java:88)

at jieMian.XmlManagerDialog.init(XmlManagerDialog.java:102)

at jieMian.XmlManagerDialog.<init>(XmlManagerDialog.java:36)

at jieMian.XmlManagerDialog.<init>(XmlManagerDialog.java:44)

at jieMian.XmlManagerDialog_butn2_actionAdapter$1.run(XmlManagerDialog.java:183)

at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
有没有做过的,要怎么解决啊,急啊

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • aninfeel 2009-02-24 20:36
    已采纳

    [code="java"]
    SAXReader reader=new SAXReader();
    Document doc=reader.read(new File("F:\temp\test2.xml"));
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("gbk");//修改编码
    XMLWriter writer=new XMLWriter(new FileOutputStream("F:\temp\test3.xml"),format);
    writer.write(doc);
    writer.close();
    [/code]

    打赏 评论

相关推荐 更多相似问题