XML文件(2)--使用DOM4J示例

  • 其他依赖字段/方法
  1. private List<Book> bookList = new LinkedList<Book>();
  2. public List<Book> getBookList(String file) {
  3. parseXML(file);
  4. return this.bookList;
  5. }
  • 获取DocumentBuilder(文件解析器)
  1. /**
  2. * 解析XML文件
  3. * * @param file
  4. */
  5. private void parseXML(String file) {
  6. // 创建book临时变量
  7. Book book = null;
  8. // 1.创建SAXReader对象
  9. SAXReader reader = new SAXReader();
  10. // 2.1 创建输入流
  11. InputStreamReader input = null;
  12. try {
  13. // 2.2 实例化输入流,并指定编码
  14. input = new InputStreamReader(new FileInputStream(file), "utf-8");
  15. // 3.加载XML文件到 document
  16. Document document = reader.read(input);
  17. // 4.获取根节点
  18. Element rootElement = document.getRootElement();
  19. // 5.根据根节点获取子节点迭代器,并遍历
  20. Iterator bookIter = rootElement.elementIterator();
  21. while (bookIter.hasNext()) {
  22. // 初始化book对象
  23. book = new Book();
  24. // 声明子节点对象
  25. Element bookEle = (Element) bookIter.next();
  26. // 如果子节点为book
  27. if (bookEle.getName().equals("book")) {
  28. // 6.获取book节点的属性迭代器,并遍历
  29. Iterator eleIter = bookEle.attributeIterator();
  30. while (eleIter.hasNext()) {
  31. // 声明属性对象
  32. Attribute attr = (Attribute) eleIter.next();
  33. if (attr.getName().equals("id")) {
  34. book.setId(Integer.valueOf(attr.getValue()));
  35. }
  36. }
  37. // 7.获取book节点的子节点迭代器,并遍历
  38. Iterator childIter = bookEle.elementIterator();
  39. while (childIter.hasNext()) {
  40. // 声明book子节点对象
  41. Element child = (Element) childIter.next();
  42. // 判断并为book对象赋值
  43. switch (child.getName()) {
  44. case "title":
  45. book.setTitle(child.getText());
  46. break;
  47. case "author":
  48. book.setAuthor(child.getText());
  49. break;
  50. case "price":
  51. book.setPrice(Double.valueOf(child.getText()));
  52. break;
  53. case "year":
  54. book.setYear(child.getText());
  55. break;
  56. case "language":
  57. book.setLanguage(child.getText());
  58. break;
  59. default:
  60. System.out.println("未知属性!");
  61. }
  62. }
  63. // 向书籍列表添加书籍对象
  64. bookList.add(book);
  65. }
  66. }
  67. } catch (UnsupportedEncodingException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. } catch (FileNotFoundException e) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace();
  73. } catch (DocumentException e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. } finally {
  77. if (null != input) {
  78. try {
  79. input.close();
  80. } catch (IOException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. }
  85. }
  86. }
  • 生成XML文件
  1. /**
  2. * 创建XML文件,保存book列表
  3. * @param bookList
  4. * @param fileName
  5. * @param encoding
  6. */
  7. private void createXML1(List<Book> bookList, String fileName,
  8. String encoding) {
  9. // 1.创建Document文件
  10. Document document = DocumentHelper.createDocument();
  11. // 2.创建root节点
  12. Element root = document.addElement("bookstore");
  13. // 3.创建book节点
  14. for (Book book : bookList) {
  15. Element ebook = root.addElement("book");
  16. // 4.1为book节点添加属性
  17. ebook.addAttribute("id", String.valueOf(book.getId()));
  18. // 4.2为book增加子节点,并设置文本值
  19. Element eTitle = ebook.addElement("title");
  20. eTitle.addCDATA(book.getTitle());
  21. Element eAuthor = ebook.addElement("author");
  22. eAuthor.setText(book.getAuthor());
  23. Element eYear = ebook.addElement("year");
  24. eYear.setText(book.getYear());
  25. Element ePrice = ebook.addElement("ePrice");
  26. ePrice.setText(String.valueOf(book.getPrice()));
  27. Element eLanguage = ebook.addElement("language");
  28. eLanguage.setText(book.getLanguage());
  29. }
  30. // 5.声明输出流
  31. OutputStream output = null;
  32. XMLWriter writer = null;
  33. // 6.设置输出的XML格式
  34. OutputFormat format = OutputFormat.createPrettyPrint();
  35. // 7.设置XML文档编码
  36. format.setEncoding(encoding);
  37. try {
  38. // 8.实例化输出流
  39. output = new FileOutputStream(fileName);
  40. // 9.实例化XML输出流,加载XML格式及设置
  41. writer = new XMLWriter(output, format);
  42. // 10.向硬盘写入XML
  43. writer.write(document);
  44. } catch (FileNotFoundException e) {
  45. e.printStackTrace();
  46. } catch (UnsupportedEncodingException e) {
  47. e.printStackTrace();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. } finally {
  51. if (null != output) {
  52. try {
  53. output.close();
  54. } catch (IOException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. }
  59. if (null != writer) {
  60. try {
  61. writer.close();
  62. } catch (IOException e) {
  63. // TODO Auto-generated catch block
  64. e.printStackTrace();
  65. }
  66. }
  67. }
  68. }
  • 创建RSS文件
  1. /**
  2. * 创建RSS文件
  3. *
  4. * @param fileName
  5. */
  6. private void createXML(String fileName) {
  7. // 1.创建document对象,代表XML文件
  8. Document document = DocumentHelper.createDocument();
  9. // 2.创建根节点
  10. Element rss = document.addElement("rss");
  11. // 3.为rss节点添加version属性(版本)
  12. rss.addAttribute("version", "2");
  13. // 3.1生成子节点及节点内容
  14. Element channel = rss.addElement("channel");
  15. // 3.2生成内容
  16. Element title = channel.addElement("title");
  17. title.setText("<![CDATA[国内最新新闻]]>");
  18. // 4.创建xml输出流
  19. File file = new File(fileName);
  20. XMLWriter writer = null;
  21. try {
  22. // 5.设置xml格式
  23. OutputFormat format = OutputFormat.createPrettyPrint();
  24. // 设置xml编码
  25. format.setEncoding("GBK");
  26. writer = new XMLWriter(new FileOutputStream(file), format);
  27. // 设置是否转义,默认为true(转义)
  28. writer.setEscapeText(false);
  29. // 6.将XML文件写到硬盘
  30. writer.write(document);
  31. } catch (UnsupportedEncodingException | FileNotFoundException e) {
  32. // TODO Auto-generated catch block
  33. e.printStackTrace();
  34. } catch (IOException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. } finally {
  38. if (null != writer) {
  39. try {
  40. writer.close();
  41. } catch (IOException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. }


posted @ 2017-01-06 11:02  易枫  阅读(256)  评论(0编辑  收藏  举报