欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

dom4j 输出中文

程序员文章站 2024-03-20 11:12:46
...

简单示例:

xml输出为

<?xml version="1.0" encoding="utf-8"?>

<books>
  <book 作者="周国平" 出版社="东方出版社">守望的距离</book>
</books>

 Java代码为

	public static void createSimpleXml(){
		Document doc = DocumentHelper.createDocument();

		Element books = doc.addElement("books");

		Element book = books.addElement("book");
		book.addAttribute("作者", "周国平");
		book.addAttribute("出版社", "东方出版社");
		book.addText("守望的距离");

		try {
			OutputFormat format = new OutputFormat("  ", true, "utf-8"); // 设置输出格式为空两格,换行,编码为utf-8
			XMLWriter writer = new XMLWriter(new OutputStreamWriter(
					new FileOutputStream("d:/books.xml"), "utf-8"), format);//创建输出对象
			writer.write(doc);
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 

 

相关标签: 出版 XML