I am using JAXP to generate and parse an XML document from which some fields are loaded from a database.

我正在使用JAXP生成和解析XML文檔,其中從數據庫中加載了一些字段。

Code to serialize the XML:

序列化XML的代碼:

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document doc = builder.newDocument();

Element root = doc.createElement("test");

root.setAttribute("version", text);

doc.appendChild(root);

DOMSource domSource = new DOMSource(doc);

TransformerFactory tFactory = TransformerFactory.newInstance();

FileWriter out = new FileWriter("test.xml");

Transformer transformer = tFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

transformer.transform(domSource, new StreamResult(out));

Code to parse the XML:

解析XML的代碼:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);

DocumentBuilder builder = factory.newDocumentBuilder();

Document doc = builder.parse("test.xml");

And I encounter the following exception:

我遇到了以下的例外:

[Fatal Error] test.xml:1:4: Invalid byte 1 of 1-byte UTF-8 sequence.

Exception in thread "main" org.xml.sax.SAXParseException: Invalid byte 1 of 1-byte UTF-8 sequence.

at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)

at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)

at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)

at com.test.Test.xml(Test.java:27)

at com.test.Test.main(Test.java:55)

The String text includes u-umlaut and o-umlaut (character codes 0xFC and 0xF6). These are the characters that are causing the error. When I escape the String myself to use ü and ö then the problem goes away. Other entities are automatically encoded when I write out the XML.

字符串文本包括u-umlaut和o-umlaut(字符代碼0xFC和0xF6)。這些是導致錯誤的字符。當我自己將字符串轉義為ü時;和& # xF6;然后問題就消失了。當我寫出XML時,其他實體會被自動編碼。

How do I get my output to be written / read properly without substituting these characters myself?

我怎樣才能在不使用這些字符的情況下正確地編寫/讀取輸出?

(I've read the following questions already:

(我已經讀過以下問題:

如何將字符從Oracle編碼到XML?

修復XML文件中的錯誤編碼)

2 个解决方案

#1

31

Use a FileOutputStream rather than a FileWriter.

使用FileOutputStream而不是FileWriter。

The latter applies its own encoding, which is almost certainly not UTF-8 (depending on your platform, it's probably Windows-1252 or IS-8859-1).

后者應用自己的編碼,幾乎肯定不是UTF-8(根據您的平台,可能是Windows-1252或is -8859-1)。

Edit (now that I have some time):

編輯(現在我有時間了):

An XML document without a prologue is permitted to be encoded as UTF-8 or UTF-16. With a prologue, it iss allowed to specify its encoding (the prologue can contain only US-ASCII characters, so prologue is always readable).

沒有序言的XML文檔可以編碼為UTF-8或UTF-16。通過序言,iss允許指定它的編碼(序言只能包含US-ASCII字符,所以序言總是可讀的)。

A Reader deals with characters; it will decode the byte stream of the underlying InputStream. As a result, when you pass a Reader to the parser, you are telling it that you've already handled the encoding, so the parser will ignore the prologue. When you pass an InputStream (which reads bytes), it does not make this assumption, and will look to the prologue to define the encoding -- or default to UTF-8/UTF-16 if it's not there.

讀者處理字符;它將解碼底層InputStream的字節流。因此,當您將閱讀器傳遞給解析器時,您是在告訴它您已經處理了編碼,因此解析器將忽略序言。當您傳遞一個InputStream(讀取字節)時,它不會做這樣的假設,並將查看序言以定義編碼——如果沒有UTF-8/UTF-16,則默認為UTF-8/UTF-16。

I've never tried reading a file that is encoded in UTF-16. I suspect that the parser will look for a Byte Order Mark (BOM) as the first 2 bytes of the file.

我從來沒有讀過用UTF-16編碼的文件。我懷疑解析器將查找字節順序標記(BOM)作為文件的前兩個字節。

#2

5

Well, for sure 0xFC and 0xF6 are not valid UTF-8 characters. These should have been finnesed to the two byte sequences: 0x3CBC and 0x3CB6.

當然,0xFC和0xF6不是有效的UTF-8字符。應該對這兩個字節序列進行檢查:0x3CBC和0x3CB6。

Most likely the problem is with the original source of the characters being defined as UTF-8 when they are not.

最可能的問題是字符的原始來源被定義為UTF-8,當它們不是。

java 读utf-8 xml_用Java和UTF-8編碼生成有效的XML。相关推荐

  1. go语言读文件 java读文件_如何从Java中的文本文件逐行读取

    我正在尝试使用Java逐行读取文本文件.这是以下方法: public void cartispopulated() throws Exception { File file = new File(&q ...

  2. java读mysql增量_在Java中检索MySQL自动增量

    我试图从数据库检索idPatient,但我不断收到错误,idPatient是自动增量.但我不知道这是什么原因造成的问题. 这是我正在尝试的代码 public List getPatientsFromD ...

  3. Java读带有BOM的UTF-8文件乱码原因及解决方法(转)

    转载:http://www.linuxidc.com/Linux/2012-12/76707.htm 最近在处理文件时发现了同样类型的文件使用的编码可能是不同的.所以想将文件的格式统一一下(因为UTF ...

  4. Java读取UTF-8格式txt文件第一行出现乱码及解决;Java读带有BOM的UTF-8文件乱码原因及解决方法(转载)...

    原文地址:http://blog.csdn.net/jackpk/article/details/5702964/ Java读取UTF-8的txt文件第一行出现乱码"?"及解决 t ...

  5. Java读带有BOM的UTF-8文件乱码原因及解决方法

    Java读带有BOM的UTF-8文件乱码原因及解决方法 Java读带有BOM的UTF-8文件乱码原因及解决方法 - daimojingdeyu - ITeye技术网站 Java读带有BOM的UTF-8 ...

  6. 网易严选Java开发三面面经:java读文件内容

    一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...

  7. Java读 写文本_java读文件写文件的方法

    java读文件写文件的方法 有的时候经常为真么读写文件最合理发愁,因为JAVA提过读写文件的方式太多了(C更甚至,fopen & open又有多少人傻傻分不清,更别说ReadFile了).今天 ...

  8. java大文件读,java 读大文件报错

    代码: import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import ...

  9. java读写锁死锁例子_Java并发关于重入锁与读写锁的详解

    这篇文章主要介绍了Java并发编程之重入锁与读写锁,文中相关实例代码详细,测试可用,具有一定参考价值,需要的朋友可以了解下. 重入锁 重入锁,顾名思义,就是支持重进入的锁,它表示该锁能够支持一个线程对 ...

最新文章

  1. Android EdgeEffect 使用 和 源码解析
  2. JAVA并发编程学习笔记------FutureTask
  3. 那些年我们一起追过的Shell Script
  4. NAACL 2019 | 一种考虑缓和KL消失的简单VAE训练方法
  5. 并发编程中常见的锁机制:乐观锁、悲观锁、CAS、自旋锁、互斥锁、读写锁
  6. java中时间各种格式
  7. python是干嘛的-python到底拿来干什么
  8. 我的android学习脚步----------- 的第一个应用
  9. 判断是否用户开启了定位服务
  10. matlab黄金分割实例,实战案例教你黄金分割
  11. 学计算机要具备什么能力,具备什么特质能学计算机
  12. 转:make cmake和catkin_make的区别
  13. 图表xAxis文字竖排显示
  14. java牛奶订购系统,Java IO系统
  15. docker制作tomcat镜像并部署war包
  16. 用微笑面对一切令你烦恼的人或事,不久之后,你会发现令你微笑的人或事变得越来越多了。
  17. Hybird App混合移动应用开发技术整体解决方案,速来围观!
  18. 视频和图片的相互转换
  19. tortoiseGit管理的文件没有绿色红色等图标
  20. (时间篇)关于时间的思考

热门文章

  1. python画动态图代码-Python使用matplotlib画动态图
  2. python3.6.2安装教程-Linux下升级安装Python-3.6.2版本
  3. python学费多少-Python开发学费一般多少钱?
  4. python培训班时间 费用-python培训班费用在多少?
  5. python小程序源代码-Python数据库小程序源代码
  6. python 书 推荐-推荐几本Python书
  7. python100个必背知识-学Python必须背的42个常见单词,看看你都会吗?
  8. 如何获取python的当前工作目录-python笔记(一)获取当前目录路径和文件
  9. 把ros2录制的bag包转换为ros1格式的bag包
  10. html5 svg 电路图,HTML5 SVG 分子结构图