private List<Book> bookList = new LinkedList<Book>(); public List<Book> getBookList(String file) { parseXML(file); return this.bookList; }
/** * 解析XML文件 * * @param file */ private void parseXML(String file) { // 建立book臨時變量 Book book = null; // 1.建立SAXReader對象 SAXReader reader = new SAXReader(); // 2.1 建立輸入流 InputStreamReader input = null; try { // 2.2 實例化輸入流,並指定編碼 input = new InputStreamReader(new FileInputStream(file), "utf-8"); // 3.加載XML文件到 document Document document = reader.read(input); // 4.獲取根節點 Element rootElement = document.getRootElement(); // 5.根據根節點獲取子節點迭代器,並遍歷 Iterator bookIter = rootElement.elementIterator(); while (bookIter.hasNext()) { // 初始化book對象 book = new Book(); // 聲明子節點對象 Element bookEle = (Element) bookIter.next(); // 若是子節點爲book if (bookEle.getName().equals("book")) { // 6.獲取book節點的屬性迭代器,並遍歷 Iterator eleIter = bookEle.attributeIterator(); while (eleIter.hasNext()) { // 聲明屬性對象 Attribute attr = (Attribute) eleIter.next(); if (attr.getName().equals("id")) { book.setId(Integer.valueOf(attr.getValue())); } } // 7.獲取book節點的子節點迭代器,並遍歷 Iterator childIter = bookEle.elementIterator(); while (childIter.hasNext()) { // 聲明book子節點對象 Element child = (Element) childIter.next(); // 判斷併爲book對象賦值 switch (child.getName()) { case "title": book.setTitle(child.getText()); break; case "author": book.setAuthor(child.getText()); break; case "price": book.setPrice(Double.valueOf(child.getText())); break; case "year": book.setYear(child.getText()); break; case "language": book.setLanguage(child.getText()); break; default: System.out.println("未知屬性!"); } } // 向書籍列表添加書籍對象 bookList.add(book); } } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != input) { try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
/** * 建立XML文件,保存book列表 * @param bookList * @param fileName * @param encoding */ private void createXML1(List<Book> bookList, String fileName, String encoding) { // 1.建立Document文件 Document document = DocumentHelper.createDocument(); // 2.建立root節點 Element root = document.addElement("bookstore"); // 3.建立book節點 for (Book book : bookList) { Element ebook = root.addElement("book"); // 4.1爲book節點添加屬性 ebook.addAttribute("id", String.valueOf(book.getId())); // 4.2爲book增長子節點,並設置文本值 Element eTitle = ebook.addElement("title"); eTitle.addCDATA(book.getTitle()); Element eAuthor = ebook.addElement("author"); eAuthor.setText(book.getAuthor()); Element eYear = ebook.addElement("year"); eYear.setText(book.getYear()); Element ePrice = ebook.addElement("ePrice"); ePrice.setText(String.valueOf(book.getPrice())); Element eLanguage = ebook.addElement("language"); eLanguage.setText(book.getLanguage()); } // 5.聲明輸出流 OutputStream output = null; XMLWriter writer = null; // 6.設置輸出的XML格式 OutputFormat format = OutputFormat.createPrettyPrint(); // 7.設置XML文檔編碼 format.setEncoding(encoding); try { // 8.實例化輸出流 output = new FileOutputStream(fileName); // 9.實例化XML輸出流,加載XML格式及設置 writer = new XMLWriter(output, format); // 10.向硬盤寫入XML writer.write(document); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != output) { try { output.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (null != writer) { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
/** * 建立RSS文件 * * @param fileName */ private void createXML(String fileName) { // 1.建立document對象,表明XML文件 Document document = DocumentHelper.createDocument(); // 2.建立根節點 Element rss = document.addElement("rss"); // 3.爲rss節點添加version屬性(版本) rss.addAttribute("version", "2"); // 3.1生成子節點及節點內容 Element channel = rss.addElement("channel"); // 3.2生成內容 Element title = channel.addElement("title"); title.setText("<![CDATA[國內最新新聞]]>"); // 4.建立xml輸出流 File file = new File(fileName); XMLWriter writer = null; try { // 5.設置xml格式 OutputFormat format = OutputFormat.createPrettyPrint(); // 設置xml編碼 format.setEncoding("GBK"); writer = new XMLWriter(new FileOutputStream(file), format); // 設置是否轉義,默認爲true(轉義) writer.setEscapeText(false); // 6.將XML文件寫到硬盤 writer.write(document); } catch (UnsupportedEncodingException | FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (null != writer) { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }