OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter xmlwriter=new XMLWriter(new FileOutputStream("F:\\bookxml.xml"),format);
把红色的部分加上
format.setEncoding("GBK");
XMLWriter xmlwriter=new XMLWriter(new FileOutputStream("F:\\bookxml.xml"),format);
把红色的部分加上
try
{
FileWriter fw = new FileWriter("test.xml"); OutputFormat format = OutputFormat.createPrettyPrint();//缩减型格式
//OutputFormat format = OutputFormat.createCompactFormat();//紧凑型格式
format.setEncoding("gb2312");//设置编码
//format.setTrimText(false);//设置text中是否要删除其中多余的空格
XMLWriter xw = new XMLWriter(fw, format);
xw.write(pm.result());
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//生成节点属性文件
Properties properties = new Properties();
String path = ServletActionContext.getServletContext().getRealPath("/")+"WEB-INF\\classes\\qpe.properties";
java.io.FileInputStream fin=new java.io.FileInputStream(path);
properties.load(fin);
//生成了水库对象实体列表
ArrayList RESList = getRESInfo(rvid);
//生成新的XML文档对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element root = doc.createElement("ROOT");
//开始组织XML文件
XMLUtil util = XMLUtil.getInstance(doc);
//生成PQE节点
String elementAttribute = properties.getProperty("QPE");
Element QPEElement = util.createElement(elementAttribute);
root.appendChild(QPEElement);
Element e = null;
//获得XML文件的任务部分
e = getTaskInfoElement(RESList,properties,doc,st,etm,QPEType);
QPEElement.appendChild(e);
//获得XML文件的参数部分
e = getCondINFOElement(RESList,properties,doc,st,et);
QPEElement.appendChild(e);
//获得XML文件的数据部分
e = getQPEInfoElement(RESList,properties,doc,rvid,st,et);
QPEElement.appendChild(e);
if(root!=null){//root节点存在开始生成文件
doc.appendChild(root);
TransformerFactory tff=TransformerFactory.newInstance();
Transformer tf=tff.newTransformer();
tf.setOutputProperty("encoding","gb2312");
DOMSource source=new DOMSource(doc);
StreamResult result=new StreamResult(new File(filePath));
tf.transform(source,result);
}
}catch(Exception e){
e.printStackTrace();
return false;
}这是我写得其中一段代码,你自己看看吧
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter output = new XMLWriter(
new FileWriter( new File("e:/catalog.xml") ),format);
output.write(document);
output.close();OutputFormat 就是专门用来进行整理格式的