生成XML方法一:
/**
* 测试生成XML文件
*/
public static void createXMLFile() {
Document document = DocumentHelper.createDocument();
Element booksElement = document.addElement("books");
booksElement.addComment("This is a test for dom4j, holen, 2004.9.11");
Element bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "yes");
Element titleElement = bookElement.addElement("title");
titleElement.setText("Dom4j Tutorials");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "yes");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene Studing");
bookElement = booksElement.addElement("book");
bookElement.addAttribute("show", "no");
titleElement = bookElement.addElement("title");
titleElement.setText("Lucene in Action");
Element ownerElement = booksElement.addElement("owner");
ownerElement.setText("O'Reilly");
try {
XMLWriter writer = new XMLWriter(new FileWriter(new File("d://jwp.xml")));
writer.write(document);
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
// String s = document.asXML();
// System.out.println(s);
// System.out.println();
// System.out.println(s.getBytes().length);
// String ss = new String(s.getBytes());
// System.out.println(ss);
// byte[] b = new byte[s.length()];
// System.out.println("十六进制:"+ByteUtils.byteToHexString(s.getBytes()));
}
生成的文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<!--This is a test for dom4j, holen, 2004.9.11-->
<book show="yes">
<title>Dom4j Tutorials</title>
</book>
<book show="yes">
<title>Lucene Studing</title>
</book>
<book show="no">
<title>Lucene in Action</title>
</book>
<owner>O'Reilly</owner>
</books>
案例:生成XML文件
最新推荐文章于 2021-12-09 17:38:03 发布