

TransformerFactory tfactory=TransformerFactory.newInstance();
try
{
Transformer transformer=tfactory.newTransformer();
DOMSource source=new DOMSource(doc);
logger.debug("New DOMSource success.");
StreamResult result=new StreamResult(new File(path));
logger.debug("New StreamResult success.");
// transformer.setOutputProperty("encoding","GBK");
transformer.setOutputProperty("encoding","gb2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");//增加换行缩进,但此时缩进默认为0
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");//设置缩进为2
transformer.transform(source,result);
}catch(TransformerConfigurationException e)
{
logger.error("Create Transformer error:"+e);
}catch(TransformerException e)
{
logger.error("Transformer XML file error:"+e);
}
原创文章,作者:苏葳,如需转载,请注明出处:https://www.swmemo.com/395.html
