Java中XML的四种解析方式(二)

三、JDOM解析

    特征:

      1、仅使用具体类,而不使用接口。

      2、API大量使用了Collections类。

 1 import org.jdom2.Attribute;
 2 import org.jdom2.Document;
 3 import org.jdom2.Element;
 4 import org.jdom2.JDOMException;
 5 import org.jdom2.input.*;
 6 import java.io.*;
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 public class JDOMTest {
11     private static ArrayList<Book> booksList = new ArrayList<Book>();
12     public static void main(String[] args) {
13         // 进行对books.xml文件的JDOM解析
14         // 准备工作
15         // 1.创建一个SAXBuilder的对象
16         SAXBuilder saxBuilder = new SAXBuilder();
17         try {
18             // 2.创建一个输入流,将xml文件加载到输入流中
19             InputStream in = new FileInputStream("src/book.xml");
20             InputStreamReader isr = new InputStreamReader(in, "UTF-8");
21             // 3.通过saxBuilder的build方法,将输入流加载到saxBuilder中
22             Document document = saxBuilder.build(isr);
23             // 4.通过document对象获取xml文件的根节点
24             Element rootElement = document.getRootElement();
25             // 5.获取根节点下的子节点的List集合
26             List<Element> bookList = rootElement.getChildren();
27             // 继续进行解析
28             for (Element book : bookList) {
29                 Book bookEntity = new Book();
30                 System.out.println("======开始解析第" + (bookList.indexOf(book) + 1)
31                         + "书======");
32                 // 解析book的属性集合
33                 List<Attribute> attrList = book.getAttributes();
34                 // 知道节点下属性名称时,获取节点值
35                 // book.getAttributeValue("id");
36                 // 遍历attrList(针对不清楚book节点下属性的名字及数量)
37                 for (Attribute attr : attrList) {
38                     // 获取属性名
39                     String attrName = attr.getName();
40                     // 获取属性值
41                     String attrValue = attr.getValue();
42                     System.out.println("属性名:" + attrName + "----属性值:"
43                             + attrValue);
44                     if (attrName.equals("id")) {
45                         bookEntity.setId(attrValue);
46                     }
47                 }
48                 // 对book节点的子节点的节点名以及节点值的遍历
49                 List<Element> bookChilds = book.getChildren();
50                 for (Element child : bookChilds) {
51                     System.out.println("节点名:" + child.getName() + "----节点值:"
52                             + child.getValue());
53                     if (child.getName().equals("name")) {
54                         bookEntity.setName(child.getValue());
55                     }
56                     else if (child.getName().equals("author")) {
57                         bookEntity.setAuthor(child.getValue());
58                     }
59                     else if (child.getName().equals("year")) {
60                         bookEntity.setYear(child.getValue());
61                     }
62                     else if (child.getName().equals("price")) {
63                         bookEntity.setPrice(child.getValue());
64                     }
65                     else if (child.getName().equals("language")) {
66                         bookEntity.setLanguage(child.getValue());
67                     }
68                 }
69                 System.out.println("======结束解析第" + (bookList.indexOf(book) + 1)
70                         + "书======");
71                 booksList.add(bookEntity);
72                 bookEntity = null;
73                 System.out.println(booksList.size());
74                 System.out.println(booksList.get(0).getId());
75                 System.out.println(booksList.get(0).getName());
76 
77             }
78         } catch (FileNotFoundException e) {
79             e.printStackTrace();
80         } catch (JDOMException e) {
81             e.printStackTrace();
82         } catch (IOException e) {
83             e.printStackTrace();
84         }
85     }
86 }
SAX解析开始
=========开始遍历某一本书的内容========
book元素的第1个属性名是:id
---属性值是:1
节点值是:冰与火之歌
节点名是:author---节点值是:乔治马丁
节点名是:year---节点值是:2014
节点名是:price---节点值是:89
===========结束遍历某一本书的内容===========
=========开始遍历某一本书的内容========
book元素的第1个属性名是:id
---属性值是:2
节点值是:安徒生童话
节点名是:year---节点值是:2004
节点名是:price---节点值是:77
节点名是:language---节点值是:English
===========结束遍历某一本书的内容===========
SAX解析结束
~!~!~!共有2本书
1
冰与火之歌
乔治马丁
2014
89
null
----finish----
2
安徒生童话
null
2004
77
English
----finish----

猜你喜欢

转载自www.cnblogs.com/churujianghudezai/p/11405408.html