import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;
public static void outputDoc (Document doc) { DOMSource doms = new DOMSource (doc); // Input. Document is an extended class of Node, and therefore can be used here.
File f = new File ("XMLOutput.txt");
StreamResult sr = new StreamResult (f); // Output. try {
TransformerFactory tf = TransformerFactory.newInstance (); // Get a TransformerFactory. Transformer t = tf.newTransformer (); // Make a new Transformer from it t.transform (doms, sr); // Use the DOMSource as input. StreamResult as output path.
}
catch (TransformerConfigurationException tce) { System.out.println ("Transformer Configuration Exception\n-----------------------"); tce.printStackTrace ();
}
catch (TransformerException te)
{ System.out.println ("Transformer Exception\n------------------");
te.printStackTrace ();
}
}
public static void outputDoc (Document doc) { DOMSource doms = new DOMSource (doc); // Input. Document is an extended class of Node, and therefore can be used here.
File f = new File ("XMLOutput.txt");
StreamResult sr = new StreamResult (f); // Output. try {
TransformerFactory tf = TransformerFactory.newInstance (); // Get a TransformerFactory. Transformer t = tf.newTransformer (); // Make a new Transformer from it t.transform (doms, sr); // Use the DOMSource as input. StreamResult as output path.
}
catch (TransformerConfigurationException tce) { System.out.println ("Transformer Configuration Exception\n-----------------------"); tce.printStackTrace ();
}
catch (TransformerException te)
{ System.out.println ("Transformer Exception\n------------------");
te.printStackTrace ();
}
}
解决方案 »
- 请教关于ant编译项目中部分java程序的问题
- 再次请求下Struts2类型转换问题,请高手帮忙看看。
- hibernate报错
- axis下的webservice问题
- JavaMail 可以开发电子邮局web端吗?
- tomcat中的Context path 起和作用 那位能详细的说下 谢谢
- Spring+hibernate中加上AOP的事物管理怎么始终出现这样的问题?????
- 请问在哪里有关于Java图形设计的电子文档
- HTML的大问题
- 大数据传输问题
- 那位用过jboss,功能如何?可以应用于多大项目?EJB比普通javabeans要好再哪些地方?
- 有关EJB在JBUILER+BAS的Deployment
public void transformDomToXml(Document document,String FileName)
{
try{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(FileName));
transformer.transform(source,result);
}catch (TransformerConfigurationException tce) {
System.out.println(" " + tce.getMessage() );
}catch (TransformerException te) {
System.out.println(" " + te.getMessage() );
}
}转贴一位仁兄的例子,忘记尊姓大名了,表示歉意与感谢!
如果你的xml文件中有中文的话,这尤其要注意。
因为DOM的默认编码是UTF8,而一般win平台下的
中文编码是gb2312或gbk,所以当这些编码写进去的时候
没什么问题,但重新用dom读出来的时候就会有编码错误。
就是原来dom对象的encoding是"GB2312"的,但是保存回去的就变成了"UTF-8",
由于保存回的文件中有中文,所以再读取这个xml文件时,分析器就会报错了。
具体的愿意应该就是楼上的cooleyes(眼睛) 所说的一样。
但是有什么办法能解决这个问题呢,能不能指定Dom的编码方式呢?
谢谢!
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("encoding","GB2312");
或者
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("encoding","GBK");
我想主要的原因是可能Java的DOM对象是用写二进制流的方法来写xml文件
把gb2312的编码写进了文件,但重新读入的时候却当作一个utf8的编码来读
这样就肯定出错了。
当然也可以采用UTF-8作为xml文件的编码
就是写进去前作编码转化
new String(s.getBytes(),"UTF-8")
如果文件的encoding指定为UTF-8,则xml如果有中文,Document dom=db.parse(file);这就一句就报错.必须指定为"GB2312"才可以.
如果再将前面提高的方法将dom保存会xml文件中.就将encoding就变成"UTF-8"
了,下一次再读的时候必然就报错了.
就是源文件里有<?xml version="1.0" encoding="GB2312"?>这一句,在这里必须是GB2312,如果是UTF-8分析器就会报错.
然后将dom保存回文件的时候,这一句就变成了<?xml version="1.0" encoding="UTF-8"?>
下次读的时候就会出错.
那是有问题的,我已经在我的工作中试过了。
我估计可能由于DOM采用二进制流来写文件
所以采用了String.getBytes()
如果你不指定编码地使用getBytes(),
则返回来的字节流就是本地编码的字节流,即GB2312或GBK编码流
最终写到文件中的就是Gb2312或GBK编码。
如果是这样的话
我上一个贴子中的第一个方法是可行的,而第二个方法
>>当然也可以采用UTF-8作为xml文件的编码
>>new String(s.getBytes(),"UTF-8")
就可能不行了,这第二种方法我没试过,大家可以试试。
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("1.xml"));
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty("encoding","GB2312");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File("2.xml"));
transformer.transform(source, result);//将所有写入XML文件
加了transformer.setOutputProperty("encoding","GB2312");
这一句确实可以了.同时也感谢其它朋友的帮助!