1:实体类
public class Book {
private Integer id;
private String name;
private String author;
private String year;
private Double price;
public Book() {
super();
}
public Book(Integer id, String name, String author, String year,
Double price) {
super();
this.id = id;
this.name = name;
this.author = author;
this.year = year;
this.price = price;
}
}2:准备类数据
public class CreatData {
public static List<Book> getData(){
List<Book> list=new ArrayList<Book>();
for (int i = 1; i < 4; i++) {
Book book =new Book(i, "猪"+i+"戒", "老"+i+"佳", "163"+i, 36.5+i);
list.add(book);
}
return list;
}
}3:生成XML文件代码
public class DomCreatXml {
public static void main(String[] args) {
new DomCreatXml().creatXml();
}
// 返回一个DocumentBuilder对象
public DocumentBuilder getDocumentBuilder() {
DocumentBuilder db = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return db;
}
// 创建xml文件
private void creatXml() {
DocumentBuilder db = getDocumentBuilder();
// 创建文档对象
Document document = db.newDocument();
// 设置xml文件中不显示 standalone="no"
document.setXmlStandalone(true);
// 创建跟节点bookstore
Element bookstore = document.createElement("bookstore");
// 获取book数据
List<Book> bookList = CreatData.getData();
// 遍历,取出list集合中的book数据
for (int i = 0; i < bookList.size(); i++) {
// 创建book节点,以及book下面的各次级节点
Element book = document.createElement("book");
Element name = document.createElement("name");
Element author = document.createElement("author");
Element year = document.createElement("year");
Element price = document.createElement("price");
// 取值
Book bk = bookList.get(i);
name.setTextContent(bk.getName());
author.setTextContent(bk.getAuthor());
year.setTextContent(bk.getYear());
price.setTextContent(bk.getPrice().toString());
/**
* 将book的子节点先添加在book下,将book节点添加到根节点bookstore下,
* 再将bookstore添加到dom树下,是一个从最子到父的逐层添加过程
*/
book.setAttribute("id", bk.getId().toString());
book.appendChild(name);
book.appendChild(author);
book.appendChild(year);
book.appendChild(price);
bookstore.appendChild(book);
}
document.appendChild(bookstore);
// 创建TransformerFactory对象
TransformerFactory tff = TransformerFactory.newInstance();
try {
Transformer tf = tff.newTransformer();
// 设置换行
tf.setOutputProperty(OutputKeys.INDENT, "yes");
// 设置生成xml文件
tf.transform(new DOMSource(document), new StreamResult(new File(
"resouce/creatBook1.xml")));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}4:效果
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>猪1戒</name>
<author>老1佳</author>
<year>1631</year>
<price>37.5</price>
</book>
<book id="2">
<name>猪2戒</name>
<author>老2佳</author>
<year>1632</year>
<price>38.5</price>
</book>
<book id="3">
<name>猪3戒</name>
<author>老3佳</author>
<year>1633</year>
<price>39.5</price>
</book>
</bookstore>
本文介绍了如何在Java中利用DOM模型来创建XML文件,详细阐述了从定义实体类到生成XML文件的步骤,适合Java开发者学习XML处理。
191

被折叠的 条评论
为什么被折叠?



