在netbeans中的包路径:
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
程序片段:
OutputFormat of=new OutputFormat(doc);
of.setEncoding("UTF-8");
of.setLineSeparator(sep);
of.setIndenting(true);
PrintWriter pw=new PrintWriter(fw); XMLSerializer xs=new XMLSerializer(fw,of);
xs.asDOMSerializer();
xs.serialize(doc.getDocumentElement()); fw.flush();
fw.close();解决方法: XMLSerializer类有两个相似的构造方法: public XMLSerializer( Writer writer, OutputFormat format )
和
public
XMLSerializer( OutputStream output, OutputFormat format ) 只要用第二个构造方法即可实现将XML编码为UTF-8格式,第一个不行。 writer会自动把String转换成系统却省的encoding。因为你的系统是中文的,所以你看到的总是GB2312或者GBK。参考资料: