public void test5() throws Exception{
SAXReader reader=new SAXReader();
Document docu=reader.read("src/book.xml");
Element root=docu.getRootElement();
Element book= (Element) root.elements().get(1);
book.addElement("零售价").setText("309");
/*方式1:
* XMLWriter(OutputStream out,OutputFormat format)
*设定格式化的编码用什么表。
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer=new XMLWriter(new FileOutputStream("src/book.xml"),format);
writer.write(docu);
writer.close();
*/
/*方式2: XMLWriter(OutputStream)
未设定
*/
XMLWriter writer=new XMLWriter(new FileOutputStream("src/book.xml"));
writer.write(docu);
writer.close();
}
上面这个方法中,两种方式的区别有上面不同呢?既然第二种方式就能实现,为什么要使用第一种呢? |
|