请教生成xml文件的中文转码问题。

spiritsl 2004-11-11 01:48:50
public class TestXml
{
public TestXml()
{
}

public static void main(String[] args)
{
String xml = "";

xml = "<?xml version='1.0' encoding='UTF-8'?>"
+ "<cobjectbean>"
+ "<typebean name='中文'/>"
+ "</cobjectbean>";

File file = new File("D:/testxml.xml");
try{
file.createNewFile();
Writer out = new FileWriter(file);
out.write(xml);
out.close();
System.out.println("success");
}
catch (IOException ex){
System.out.println("error");
}
}
}

上面是我的源码,问题是这样子的;
通过程序生成一个xml文件,但是当文件中存在中文时,生成的文件使用IE6.0打不开,
请问各位大虾有没有办法。在此谢谢先,急!!

补充一点,之所以使用IE打开,是因为生成的文件需要与其他程序(解析IE可以打开的xml)进行交互,现在其他程序都可以用了,所以不能再修改交互的标准,只能要求我生成的文件可以使用IE打开。
...全文
392 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
laughter75 2004-11-11
  • 打赏
  • 举报
回复
你的
TestXml类,只是下个有关IO的处理,不是生成xml的办法, IceCraft(心淡情浓)的办法是对的,我也是这么用的,
dom4j里也有Document 之类的对象吧,你看看API DOC 有一天时间,就能搞定!!
spiritsl 2004-11-11
  • 打赏
  • 举报
回复
可是我使用的是dom4j,怎么办呀??
will52000 2004-11-11
  • 打赏
  • 举报
回复
xml = "<?xml version='1.0' encoding='GBK'?>"
+ "<cobjectbean>"
+ "<typebean name='中文'/>"
+ "</cobjectbean>";
IceCraft 2004-11-11
  • 打赏
  • 举报
回复
通常我是使用jdom生成,然后再转码输出。
Element root = new Element("TEST");
Document doc = new Document(root);
Element node = new Element("TESTNODE");
node.setAttribute("NAME","IceCraft");
root.getChildren().add(node);
return JDomUtil.outputToString(doc,"GBK");

JDomUtil类如下:
public class JDomUtil {
* 这个方法将JDom对象转换字符串.
* @param document 将要被转换的JDom对象
* @param encoding 输出字符串使用的编码
*/
public static String outputToString(Document document,String encoding){
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
XMLOutputter docWriter = new XMLOutputter();
Format myFormat = docWriter.getFormat();
myFormat.setEncoding(encoding);
docWriter.setFormat(myFormat);
try{
docWriter.output(document, byteRep);
}catch(Exception e){
}
return byteRep.toString();
}
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧