Xml解析之PULL解析 例2

时间:2022-10-31 13:08:07
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <books>
 3 <book id="1001">
 4   <name>红楼梦</name>
 5   <author>曹雪芹</author>
 6   <price>345.6</price>
 7 </book>
 8 <book id="1002">
 9   <name>西游记</name>
10   <author>吴承恩</author>
11   <price>102.8</price>
12 </book>
13 <book id="1003">
14   <name>三国演义</name>
15   <author>罗贯中</author>
16   <price>200.6</price>
17 </book>
18 <book id="1004">
19   <name>水浒传</name>
20   <author>施耐庵</author>
21   <price>108.6</price>
22 </book>
23 </books>

例2.
1、用了Map<String, Object> book表示一个book对象
2、用了List<Map<String,Object>> list存放一组book对象
 
 1 package com.qf.pull;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 import java.util.ArrayList;
 6 import java.util.HashMap;
 7 import java.util.List;
 8 import java.util.Map;
 9 
10 import org.xmlpull.v1.XmlPullParser;
11 import org.xmlpull.v1.XmlPullParserException;
12 import org.xmlpull.v1.XmlPullParserFactory;
13 
14 public class Demo4_book {
15 
16     public static void main(String[] args) throws XmlPullParserException,
17             IOException {
18         // TODO Auto-generated method stub
19         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
20         XmlPullParser parser = factory.newPullParser();
21         parser.setInput(new FileInputStream("src/book.xml"), "utf-8");
22         int type = parser.getEventType();
23         Map<String, Object> book = null;
24         List<Map<String, Object>> list = null;
25         while (type != XmlPullParser.END_DOCUMENT) {
26             String tagName = parser.getName();
27             switch (type) {
28             //不同于例1,也可以在文档开始的时候,实例化list对象,用于存放XML解析后的数据
29             case XmlPullParser.START_DOCUMENT:
30                 list = new ArrayList<Map<String, Object>>();
31                 // System.out.println(tagName);//null
32                 break;
33             case XmlPullParser.START_TAG:
34                 // System.out.println(tagName);
35                 if ("book".equals(tagName)) {
36                     book = new HashMap<String, Object>();
37                     int count = parser.getAttributeCount();//假设有多个属性值
38                     for (int i = 0; i < count; i++) {
39                         String name = parser.getAttributeName(i);//比如"id"
40                         String value = parser.getAttributeValue(i);//比如"1004"
41                         book.put(name, value);
42                     }
43                 } else if ("name".equals(tagName)) {
44                     book.put("name", parser.nextText());
45                 } else if ("author".equals(tagName)) {
46                     book.put("author", parser.nextText());
47                 } else if ("price".equals(tagName)) {
48                     book.put("price", Double.parseDouble(parser.nextText()));
49                 }
50                 break;
51             case XmlPullParser.END_TAG:
52                 if ("book".equals(tagName)) {
53                     list.add(book);
54                 }
55                 break;
56             }
57 
58             type = parser.next();
59         }// end while
60         for (Map<String, Object> map : list) {
61             System.out.println(map);
62         }
63     }
64 }