关于使用Dom创建xml文档时的问题

amdey 2003-10-09 11:22:26
我使用xerces中的DOM来创建一个新的xml文档,如下:

Document doc=new DocumentImpl();
创建后的xml文档中默认编码方法为:

<?xml version="1.0" endocing="utf-8"?>

现在我想把utf-8用gb2312替换掉,请问在程序中应该如何处理?
...全文
38 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
swinging 2003-10-10
  • 打赏
  • 举报
回复
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
Properties properties = transformer.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING, "gb2312");//// 就是这句了
properties.setProperty(OutputKeys.METHOD, "xml");
properties.setProperty(OutputKeys.VERSION, "1.0");
properties.setProperty(OutputKeys.INDENT, "no");
transformer.setOutputProperties(properties);

DOMSource source = new DOMSource(doc);

其中最后一句中得doc是拼好得XML。比如:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element root = doc.createElement("DSTreeRoot");
..........................(就不细写了)

62,614

社区成员

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

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