为了账号安全,请及时绑定邮箱和手机立即绑定

pull解析xml文件的实现与应用

标签:
Android

1.解析的文件read.xml

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<!--?xml version="1.0"   encoding="UTF-8"?-->

<books>

    <time level="1">

        <book   id="10086">C语言程序与设计</book>

        <book   id="10010">Java通史</book>

        <book   id="10055">C++进阶宝典</book>

        <book   id="10089">Swift入门与实践</book>

    </time>

    <time level="2">

        <book   id="10090">教你怎么不生气</book>

        <book   id="10005">佛经</book>

        <book   id="10095">老子</book>

        <book   id="10389">沉默的愤怒</book>

    </time>

    <time level="3">

        <book   id="10000">腰间盘突出日常防护</book>

        <book   id="10023">保护视力从点滴开始</book>

        <book   id="10011">颈椎病康复指南</book>

        <book   id="10013">心脏病的预防与防治</book>

        <book   id="10018">高血压降压宝典</book>

        <book   id="10019">强迫症的自我恢复</book>

        <book   id="10028">精神病症状学</book>

    </time>

    <time level="4">

        <book   id="12086">活着</book>

        <book   id="16010">生命的意义</book>

    </time>

    <time level="5">

        <book   id="12345">埋葬地理的风水学</book>

        <book   id="10329">遗书及财产分配学</book>

    </time>

</books>

 

2.Book类的创建

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package demo05;

 

public class Book {

    private int id;

    private String name;

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id   = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String   name) {

        this.name   = name;

    }

    @Override

    public String toString() {

        return "Book   [id=" + id + ", name=" + name + "]";

    }

     

}

 

3.解析类

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

package demo05;

 

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Set;

 

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserException;

import org.xmlpull.v1.XmlPullParserFactory;

 

public class PullParse {

 

     

 

    public static void main(String[]   args) throws Exception {

             

        XmlPullParserFactory   factory=XmlPullParserFactory.newInstance();

        XmlPullParser   parser=factory.newPullParser();

        parser.setInput(new   FileInputStream("db/read.xml"),"UTF-8");

        int event=parser.getEventType();

        String   tag=null;

        Book   book=null;

        HashMap<integer,list<book>>map=null;

         List<book>list=null;

        while(event!=XmlPullParser.END_DOCUMENT){

             switch   (event) {

                case   XmlPullParser.START_DOCUMENT:

                    map=new   HashMap<integer, list<book="">>();

                    break;

                case   XmlPullParser.START_TAG:

                    tag=parser.getName();

                    if("time".equals(tag)){

                        String   level=parser.getAttributeValue("","level");

                        list=new   ArrayList<book>();

                        map.put(Integer.parseInt(level),list);

                    }

                    if("book".equals(tag)){

                        book=new   Book();

                        String   id=parser.getAttributeValue("","id");

                        book.setId(Integer.parseInt(id));

                    }

                    break;

                case   XmlPullParser.TEXT:

                    if("book".equals(tag)){

                        book.setName(parser.getText());

                    }

                 

                    break;

                case   XmlPullParser.END_TAG:

                    tag=null;

                    if("book".equals(parser.getName())){

                        list.add(book);

                    }

             

                    break;

            }

             event=parser.next();

        }

     

        Set<integer>set=map.keySet();

         for(Integer   in:set){

             System.out.println(in);

             for(Book   b:map.get(in)){

                 System.out.println(b);

             }

         }

    }

 

 

     

}</integer></book></integer,></book></integer,list<book>

 

原文链接:http://www.apkbus.com/blog-813041-61003.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消