当前位置: 技术问答>java相关
请问怎么利用一个dom对象生成一个xml文件?
来源: 互联网 发布时间:2015-04-29
本文导语: 用jaxp或者Xerces分析器. 谢谢! | 一个要注意的问题是xml文件的编码 如果你的xml文件中有中文的话,这尤其要注意。 因为DOM的默认编码是UTF8,而一般win平台下的 中文编码是gb2312或gbk,所以当这些...
用jaxp或者Xerces分析器.
谢谢!
谢谢!
|
一个要注意的问题是xml文件的编码
如果你的xml文件中有中文的话,这尤其要注意。
因为DOM的默认编码是UTF8,而一般win平台下的
中文编码是gb2312或gbk,所以当这些编码写进去的时候
没什么问题,但重新用dom读出来的时候就会有编码错误。
如果你的xml文件中有中文的话,这尤其要注意。
因为DOM的默认编码是UTF8,而一般win平台下的
中文编码是gb2312或gbk,所以当这些编码写进去的时候
没什么问题,但重新用dom读出来的时候就会有编码错误。
|
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 Exceptionn-----------------------"); tce.printStackTrace ();
}
catch (TransformerException te)
{ System.out.println ("Transformer Exceptionn------------------");
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 Exceptionn-----------------------"); tce.printStackTrace ();
}
catch (TransformerException te)
{ System.out.println ("Transformer Exceptionn------------------");
te.printStackTrace ();
}
}
|
与楼上的同
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() );
}
}
转贴一位仁兄的例子,忘记尊姓大名了,表示歉意与感谢!
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() );
}
}
转贴一位仁兄的例子,忘记尊姓大名了,表示歉意与感谢!