辟邪剑谱 9.9 班主任
当前位置:   article > 正文

解析XML步骤及案例_xml解析步骤

xml解析步骤

1.引入dom4j解析xml文件的jar

2.创建xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <books>
  3. <book sn="SN12341232">
  4. <name>辟邪剑谱</name>
  5. <price>9.9</price>
  6. <author>班主任</author>
  7. </book>
  8. <book sn="SN12341231">
  9. <name>葵花宝典</name>
  10. <price>99.99</price>
  11. <author>班长</author>
  12. </book>
  13. </books>

3.创建一个book类,并声明构造方法、get、set方法、toString方法

  1. package com.atguigu.pojo;
  2. import java.math.BigDecimal;
  3. public class Book {
  4. private String sn;
  5. private String name;
  6. private double price;
  7. private String author;
  8. public Book() {
  9. }
  10. public Book(String sn, String name, double price, String author) {
  11. this.sn = sn;
  12. this.name = name;
  13. this.price = price;
  14. this.author = author;
  15. }
  16. public String getSn() {
  17. return sn;
  18. }
  19. public void setSn(String sn) {
  20. this.sn = sn;
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public double getPrice() {
  29. return price;
  30. }
  31. public void setPrice(double price) {
  32. this.price = price;
  33. }
  34. public String getAuthor() {
  35. return author;
  36. }
  37. public void setAuthor(String author) {
  38. this.author = author;
  39. }
  40. @Override
  41. public String toString() {
  42. return "Book{" +
  43. "sn='" + sn + '\'' +
  44. ", name='" + name + '\'' +
  45. ", price=" + price +
  46. ", author='" + author + '\'' +
  47. '}';
  48. }
  49. }

4.编写一个解析测试类

  1. import org.dom4j.Document;
  2. import org.dom4j.Element;
  3. import org.dom4j.io.SAXReader;
  4. import org.junit.Test;
  5. import java.math.BigDecimal;
  6. import java.util.List;
  7. public class Dom4jTest {
  8. @Test
  9. public void test1() throws Exception {
  10. // 创建一个SaxReader输入流,去读取 xml配置文件,生成Document对象
  11. SAXReader saxReader = new SAXReader();
  12. Document document = saxReader.read("src/books.xml");
  13. //通过Document对象获取根元素
  14. Element rootElement = document.getRootElement();
  15. //通过根元素获取book标签对象
  16. //element()(有一个子元素)和elements()(有多个子元素)都是通过标签名查找子元素
  17. List<Element> books = rootElement.elements("book");
  18. //遍历,处理每个book标签转化为Book类
  19. for (Element book : books) {
  20. //asXML()把标签对象,转化为标签字符串
  21. Element name = book.element("name");
  22. //getText()可以获取标签中的文本内容
  23. String nameText = name.getText();
  24. System.out.println(nameText);
  25. //直接获取指定标签名的文本内容
  26. String price = book.elementText("price");
  27. String author = book.elementText("author");
  28. //获取属性值sn
  29. String sn = book.attributeValue("sn");
  30. System.out.println(price);
  31. System.out.println(author);
  32. System.out.println(sn);
  33. //转化为Book类并输出
  34. Book book1 = new Book(sn, nameText, Double.parseDouble(price), author);
  35. System.out.println(book1 );
  36. }
  37. }
  38. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/817620
推荐阅读
相关标签