XML檔案生成—DOM方式
阿新 • • 發佈:2019-01-31
1:實體類
2:準備類資料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; } }
3:生成XML檔案程式碼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; } }
4:效果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(); } } }
<?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>