利用org.w3c.dom如何读写xml部的信息,比如更改encoding或者standalone属性等

stanely 2005-03-20 02:45:52
除了标题中的两处以外,我还想知道如何通过dom读写xml文件的<?xml-stylesheet type="text/xsl" href="xxx.xsl"?>这一部分的内容。这个该了,standalone会自动变化么?

另外,对于encoding的修改,如何真正意义上改变整个文档内容的输出编码,而不是只更改这一个属性而已?

我想让我的所有xml是unicode编码的,怎么做?比如对于如下代码,Jbuilder2005中我不知道怎样才能在元代码里面输入可识别的中文,那么怎么能让这个文档的编码是unicode或者gbk呢?

Element r=doc.createElement("root");
doc.appendChild(r);
Element s1=doc.createElement("中文");
r.appendChild(s1);

还有,对于原本是unicode编码的xml文档,怎样通过dom操作,将其变成实际的iso-8859-1编码,当然encoding属性也要跟着改变了。

对于这个问题,假设里面都是英文字母,怎样做?如果包含中文,那么转换成iso-8859-1以后在转换成gbk会不会丢失信息?我不太了解java 的encoding原则,听说是默认的unicode,但是为什么如下代码生成的文件长度是1?哪位能给我详细讲一下java的编码原则呢?谢谢!!

FileOutputStream fout=new FileOutputStream("test.txt");
fout.write("a".getBytes());
fout.flush();
fout.close();
...全文
413 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
stanely 2005-03-26
  • 打赏
  • 举报
回复
谢谢帮助,但是有个问题,当我输出utf-16编码的时候文档不能被ie,xmlspy识别,说格式不对。

Document doc=....


Element r=doc.createElement("root");
r.setAttribute("sw","ss");
doc.appendChild(r);
Element s1=doc.createElement("s1");

TransformerFactory xformFactory
= TransformerFactory.newInstance();
Transformer idTransform = xformFactory.newTransformer();

idTransform.setOutputProperty(OutputKeys.ENCODING,"utf-16");

Source input = new DOMSource(doc);
Result output = new StreamResult(new FileOutputStream("c:\\1.xml"));
idTransform.transform(input, output);

////////////////////



请注意此问题!!!!!!!!!

stanely 2005-03-25
  • 打赏
  • 举报
回复
?
stanely 2005-03-23
  • 打赏
  • 举报
回复
为什么不能输出正确的utf-16编码?
OnlyFor_love 2005-03-23
  • 打赏
  • 举报
回复
GB2312和GBK都是支持中文的编码格式!
一般都用GBK的
stanely 2005-03-23
  • 打赏
  • 举报
回复
谢谢帮助,但是有个问题,当我输出utf-16编码的时候文档不能被ie,xmlspy识别,说格式不对。

Document doc=....


Element r=doc.createElement("root");
r.setAttribute("sw","ss");
doc.appendChild(r);
Element s1=doc.createElement("s1");

TransformerFactory xformFactory
= TransformerFactory.newInstance();
Transformer idTransform = xformFactory.newTransformer();

idTransform.setOutputProperty(OutputKeys.ENCODING,"utf-16");

Source input = new DOMSource(doc);
Result output = new StreamResult(new FileOutputStream("c:\\1.xml"));
idTransform.transform(input, output);
taglib 2005-03-20
  • 打赏
  • 举报
回复
一般是用OutputStreamWriter输出你需要的encoding


XML文件的encoding, 写Transformer的属性,参考

http://www.rgagnon.com/javadetails/java-0481.html


譬如

Element root = doc.getDocumentElement();

ProcessingInstruction xmlstylesheet
= doc.createProcessingInstruction("xml-stylesheet",
"type=\"text/xsl\" href=\"myxsl.xsl\"");
doc.insertBefore(xmlstylesheet, root);



TransformerFactory xformFactory
= TransformerFactory.newInstance();
Transformer idTransform = xformFactory.newTransformer();
idTransform.setOutputProperty(OutputKeys.ENCODING,"GB2312");
idTransform.setOutputProperty(OutputKeys.STANDALONE,"yes");
Source input = new DOMSource(doc);
Result output = new StreamResult(System.out);
idTransform.transform(input, output);

62,614

社区成员

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

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