JDom输出UTF 8的XML完美解决
转贴地址: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、Output
相关文档:
private
NodeList root(
final
String url ,
final
String str){
NodeList root =
null
;
try
{
InputSource is=
new
InputSource(
new
InputStreamReader(
new
UR ......
原文:使用 MSXML 分析器处理 XML 文档
#include <atlbase.h>
#include <iostream>
using namespace std;
//<?xml version="1.0"?>
//<xmldata>
//<xmlnode />
//<xmltext>Hello, World!</xmltext>
//</xmldata>
void main ......
import java.io.StringWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.dom.DOMImplementationImpl;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Serializer;
import org.apache.xml. ......
ArrayAdapter adapter =
ArrayAdapter.createfromResource(
this,
R.array.catalog,
android.R.layout.simple_list_item_1);
this.setListAdapter(adapter);
xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name= ......
我写的第一个程序中写XML的代码执行速度有些问题,改了一下,现在有所改善。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
......