JDom输出UTF-8的XML完美解决
2010-04-20 11:12
316 查看
转贴地址:http://java.chinaitlab.com/advance/755393.html
现象描述:JDom输出Xml文件,当使用字符编码GBK时正常,而输出UTF-8时乱码。
完美的解决方法从辟谣开始:
1)JDOM是否生成UTF-8的文件与Format是否设置无关,只有输出其他字符编码才需要设置,见下面的注释。
2)JDOM输出UTF-8文件乱码的根本原因并非在JDOMAPI,而是在JDK。
具体描述:
JDOM的输出类XMLOutputter有两个output接口,除了都具有一个Document参数外,分别接受Writer和OutputStream参数。
这给我们一个错觉,两个接口可以任意使用。
首先我们用output(doc,System.out)来做测试,此时得到乱码,
然后我们改为output(doc,new PrintWriter(System.out))来测试,输出不是乱码,
也就是说在控制台的时候一定要用一个Writer接口包装一下。
然后我们用output(doc,new FileWriter(path))来做测试,结果却得到乱码,
然后我们改为output(doc,new FileOutputStream(path))来测试,输出不是乱码,
也就是说在输出文件的时候一定要用一个OutputStream接口包装一下。
疯了吧?呵呵,很搞笑是吧。经过到JDOM的源码中调试,发现没有任何问题,问题出在了JDK里面。
JDK内的对应接口处理:
1)PrintWriter类有参数为OutputStream的构造方法,因此可以从System.out包装到PrintWriter
2)FileWriter类没有参数为OutputStream的构造方法,因此不能从FileOutputStream包装到FileWriter
3)如果PrintWriter类用了参数为Writer的构造方法(Writer实现为FileWriter),最后输出也是乱码
4)如果用一个FileOutputStream来包装一个控制台输出,也是乱码
因此,对于JDK内的各种输出体系,各种InputStream、OutputStream、reader和writer要充分认识,否则极容易出现一些意想不到的问题。
测试的JDOM版本:1.0、1.1
测试代码:
view plaincopy to clipboardprint?
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class BuildXML {
public static void main(String[] args) throws Exception{
File xmlfile=new File("C://EditTemp//xml//abc.xml");
//中文问题 //GBK 是没有问题的,但UTF-8就是有问题的
//原因:
//1)对于磁盘文件,必须使用输出流 FileOutputStream
// FileWriter out=new FileWriter(xmlfile);会导致乱码
//2)对于控制台输出,则必须使用PrintWriter,如果直接使用System.out也会出现乱码
// PrintWriter out=new PrintWriter(System.out);
FileOutputStream out=new FileOutputStream(xmlfile);
Element eroot=new Element("root");
eroot.addContent((new Element("code")).addContent("代码"));
eroot.addContent((new Element("ds")).addContent("数据源"));
eroot.addContent((new Element("sql")).addContent("检索sql"));
eroot.addContent((new Element("order")).addContent("排序"));
Document doc=new Document(eroot);
XMLOutputter outputter = new XMLOutputter();
//如果不设置format,仅仅是没有缩进,xml还是utf-8的,因此format不是必要的
Format f = Format.getPrettyFormat();
//f.setEncoding("UTF-8");//default=UTF-8
outputter.setFormat(f);
outputter.output(doc, out);
out.close();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class BuildXML {
public static void main(String[] args) throws Exception{
File xmlfile=new File("C://EditTemp//xml//abc.xml");
//中文问题 //GBK 是没有问题的,但UTF-8就是有问题的
//原因:
//1)对于磁盘文件,必须使用输出流 FileOutputStream
// FileWriter out=new FileWriter(xmlfile);会导致乱码
//2)对于控制台输出,则必须使用PrintWriter,如果直接使用System.out也会出现乱码
// PrintWriter out=new PrintWriter(System.out);
FileOutputStream out=new FileOutputStream(xmlfile);
Element eroot=new Element("root");
eroot.addContent((new Element("code")).addContent("代码"));
eroot.addContent((new Element("ds")).addContent("数据源"));
eroot.addContent((new Element("sql")).addContent("检索sql"));
eroot.addContent((new Element("order")).addContent("排序"));
Document doc=new Document(eroot);
XMLOutputter outputter = new XMLOutputter();
//如果不设置format,仅仅是没有缩进,xml还是utf-8的,因此format不是必要的
Format f = Format.getPrettyFormat();
//f.setEncoding("UTF-8");//default=UTF-8
outputter.setFormat(f);
outputter.output(doc, out);
out.close();
}
}
另附一方法,将JDOM的Document对象,根据指定的编码输出字符串:
view plaincopy to clipboardprint?
/**
* 这个方法将JDom Document对象根据指定的编码转换字符串返回。
* @param xmlDoc 将要被转换的JDom对象
* @param encoding 输出字符串使用的编码
* @return String Document经处理生成的字符串
* @throws IOException
*/
public static String toXML(Document xmlDoc, String encoding) throws IOException
{
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
PrintWriter out=new PrintWriter(byteRep);
Format format = Format.getPrettyFormat();
format.setEncoding(encoding);
XMLOutputter docWriter = new XMLOutputter(format);
try {
docWriter.output(xmlDoc, out);
} catch (Exception e) {
}
return byteRep.toString();
}
现象描述:JDom输出Xml文件,当使用字符编码GBK时正常,而输出UTF-8时乱码。
完美的解决方法从辟谣开始:
1)JDOM是否生成UTF-8的文件与Format是否设置无关,只有输出其他字符编码才需要设置,见下面的注释。
2)JDOM输出UTF-8文件乱码的根本原因并非在JDOMAPI,而是在JDK。
具体描述:
JDOM的输出类XMLOutputter有两个output接口,除了都具有一个Document参数外,分别接受Writer和OutputStream参数。
这给我们一个错觉,两个接口可以任意使用。
首先我们用output(doc,System.out)来做测试,此时得到乱码,
然后我们改为output(doc,new PrintWriter(System.out))来测试,输出不是乱码,
也就是说在控制台的时候一定要用一个Writer接口包装一下。
然后我们用output(doc,new FileWriter(path))来做测试,结果却得到乱码,
然后我们改为output(doc,new FileOutputStream(path))来测试,输出不是乱码,
也就是说在输出文件的时候一定要用一个OutputStream接口包装一下。
疯了吧?呵呵,很搞笑是吧。经过到JDOM的源码中调试,发现没有任何问题,问题出在了JDK里面。
JDK内的对应接口处理:
1)PrintWriter类有参数为OutputStream的构造方法,因此可以从System.out包装到PrintWriter
2)FileWriter类没有参数为OutputStream的构造方法,因此不能从FileOutputStream包装到FileWriter
3)如果PrintWriter类用了参数为Writer的构造方法(Writer实现为FileWriter),最后输出也是乱码
4)如果用一个FileOutputStream来包装一个控制台输出,也是乱码
因此,对于JDK内的各种输出体系,各种InputStream、OutputStream、reader和writer要充分认识,否则极容易出现一些意想不到的问题。
测试的JDOM版本:1.0、1.1
测试代码:
view plaincopy to clipboardprint?
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class BuildXML {
public static void main(String[] args) throws Exception{
File xmlfile=new File("C://EditTemp//xml//abc.xml");
//中文问题 //GBK 是没有问题的,但UTF-8就是有问题的
//原因:
//1)对于磁盘文件,必须使用输出流 FileOutputStream
// FileWriter out=new FileWriter(xmlfile);会导致乱码
//2)对于控制台输出,则必须使用PrintWriter,如果直接使用System.out也会出现乱码
// PrintWriter out=new PrintWriter(System.out);
FileOutputStream out=new FileOutputStream(xmlfile);
Element eroot=new Element("root");
eroot.addContent((new Element("code")).addContent("代码"));
eroot.addContent((new Element("ds")).addContent("数据源"));
eroot.addContent((new Element("sql")).addContent("检索sql"));
eroot.addContent((new Element("order")).addContent("排序"));
Document doc=new Document(eroot);
XMLOutputter outputter = new XMLOutputter();
//如果不设置format,仅仅是没有缩进,xml还是utf-8的,因此format不是必要的
Format f = Format.getPrettyFormat();
//f.setEncoding("UTF-8");//default=UTF-8
outputter.setFormat(f);
outputter.output(doc, out);
out.close();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class BuildXML {
public static void main(String[] args) throws Exception{
File xmlfile=new File("C://EditTemp//xml//abc.xml");
//中文问题 //GBK 是没有问题的,但UTF-8就是有问题的
//原因:
//1)对于磁盘文件,必须使用输出流 FileOutputStream
// FileWriter out=new FileWriter(xmlfile);会导致乱码
//2)对于控制台输出,则必须使用PrintWriter,如果直接使用System.out也会出现乱码
// PrintWriter out=new PrintWriter(System.out);
FileOutputStream out=new FileOutputStream(xmlfile);
Element eroot=new Element("root");
eroot.addContent((new Element("code")).addContent("代码"));
eroot.addContent((new Element("ds")).addContent("数据源"));
eroot.addContent((new Element("sql")).addContent("检索sql"));
eroot.addContent((new Element("order")).addContent("排序"));
Document doc=new Document(eroot);
XMLOutputter outputter = new XMLOutputter();
//如果不设置format,仅仅是没有缩进,xml还是utf-8的,因此format不是必要的
Format f = Format.getPrettyFormat();
//f.setEncoding("UTF-8");//default=UTF-8
outputter.setFormat(f);
outputter.output(doc, out);
out.close();
}
}
另附一方法,将JDOM的Document对象,根据指定的编码输出字符串:
view plaincopy to clipboardprint?
/**
* 这个方法将JDom Document对象根据指定的编码转换字符串返回。
* @param xmlDoc 将要被转换的JDom对象
* @param encoding 输出字符串使用的编码
* @return String Document经处理生成的字符串
* @throws IOException
*/
public static String toXML(Document xmlDoc, String encoding) throws IOException
{
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
PrintWriter out=new PrintWriter(byteRep);
Format format = Format.getPrettyFormat();
format.setEncoding(encoding);
XMLOutputter docWriter = new XMLOutputter(format);
try {
docWriter.output(xmlDoc, out);
} catch (Exception e) {
}
return byteRep.toString();
}
相关文章推荐
- 【原创】JDom输出UTF-8的XML完美解决(中文乱码的原因分析)
- JDom输出UTF-8的XML完美解决
- JDom输出UTF-8的XML出现中文乱码完美解决
- JDom输出UTF-8的XML完美解决
- JDOM输出UTF-8解决方法
- 解决jdom遍历xml时输出节点顺序错误的问题
- JDOM输出XML字符串的属性
- VBA 格式化输出XML(UTF-8无BOM编码)
- c#以文件流的形式输出xml(可以解决内存溢出)-XmlTextWriter
- iOS xmpp一定编译成功的安装版本,完美解决Module 'libxmlSimu' not found。
- 打造完美的xml技术解决方案件(dom4j/xstream)
- Asp 输出编码为UTF-8格式的XML内容的注意事项
- 用dom4j建立,修改XML文档,并解决格式化输出和中文问题
- utf-8 转为 gb2312 和 smartupload上传文件并附text域的值的汉字解决办法(注意是:WAP,XHTML或XML中)
- java--解决文件输出流保存内容无中文时文件编码为ANSI格式不是utf-8
- 完美解决mysql下utf-8的乱码问题
- 完美解决mysql下utf-8的乱码问题
- utf-8编码引起document.writeln输出中文乱码的解决办法
- php输出xml必须header的解决方法
- Sublime text 2/3 [Decode error - output not utf-8] 完美解决方法