这里只提供案例,也可参考解析xml之DOM4J,详细了解DOM4J
代码如下:
//首先创建一个Document对象
Document document = DocumentHelper.createDocument();
Element bookstore = document.addElement("bookstore");
Element book = bookstore.addElement("book");
book.addAttribute("id", "1");
Element book_name= book.addElement("name");
book_name.setText("人性的弱点//优势");
//设置xml 生成格式
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
format.setIndent(true);
File file = new File("src/dom4j_bookstore.xml");
try {
XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);
// 当节点值中包含<> // 这些转义字符时,可以通过设置文本是否转义来显示,默认为true, 代表转义
writer.setEscapeText(false);
writer.write(document);
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}