xml解析,基于XmlPullParser

本文介绍如何使用XML Pull Parser解析XML文件。通过示例代码详细解释了关键方法next()和nextToken()的功能,以及如何通过getEventType()判断解析器的状态。此外,还提供了一个简单的应用程序示例,展示了如何遍历XML文档并提取所需的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解析xml文件

XML Pull Parser is an interface that defines parsing
 functionality provided in XMLPULL V1 API (visit this 
 website to learn more about API and its implementations).

There are two key methods: next() and nextToken(). While next() provides 
access to high level parsing events, nextToken() allows access to lower level
 tokens.

The current event state of the parser can be determined by calling the
 getEventType() method. Initially, the parser is in the START_DOCUMENT
  state.
The method next() advances the parser to the next event. The int
 value returned from next determines the current parser state and is identical
  to the value returned from following calls to getEventType ()


Th following event types are seen by next()
START_TAG
An XML start tag was read.

TEXT
Text content was read; the text content can be retrieved using the getText() 
method. (when in validating mode next() will not report ignorable 
whitespace, use nextToken() instead)

END_TAG
An end tag was read

END_DOCUMENT
No more events are available

A minimal example for using this API may look as follows:


   import java.io.IOException;
   import java.io.StringReader;

   import org.xmlpull.v1.XmlPullParser;
   import org.xmlpull.v1.XmlPullParserException;
   import org.xmlpull.v1.XmlPullParserFactory;

   public class SimpleXmlPullApp
   {

       public static void main (String args[])
           throws XmlPullParserException, IOException
       {
           XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
           factory.setNamespaceAware(true);
           XmlPullParser xpp = factory.newPullParser();

           xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
           int eventType = xpp.getEventType();
           while (eventType != XmlPullParser.END_DOCUMENT) {
            if(eventType == XmlPullParser.START_DOCUMENT) {
                System.out.println("Start document");
            } else if(eventType == XmlPullParser.START_TAG) {
                System.out.println("Start tag "+xpp.getName());
            } else if(eventType == XmlPullParser.END_TAG) {
                System.out.println("End tag "+xpp.getName());
            } else if(eventType == XmlPullParser.TEXT) {
                System.out.println("Text "+xpp.getText());
            }
            eventType = xpp.next();
           }
           System.out.println("End document");
       }
   }

1,获取 XmlPullParser

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = factory.newPullParser();

或者:

XmlPullParser parser = Xml.newPullParser();

2,把获取的流,加载到parser里面

parser.setInput(is, "utf-8");

3,pull解析基于事件驱动的,定义一个:

 int eventType = parser.getEventType();
eventType != XmlPullParser.END_DOCUMENT  //获取开始
parser.getName();//获取节点名称
parser.nextText()//获取节点下值
parser.next();//取下一个事件类型
  XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "utf-8");

        int eventType = parser.getEventType();

        List<NewInfo> newInfos = null;
        NewInfo newInfo = null;
        while (eventType != XmlPullParser.END_DOCUMENT) {
            String tagName = parser.getName();//节点名称
            switch (eventType) {
                case XmlPullParser.START_TAG://<news>:
                    if ("news".equals(tagName)) {
                        newInfoList = new ArrayList<NewInfo>();
                    } else if ("new".equals(tagName)) {
                        newInfo = new NewInfo();
                    } else if ("title".equals(tagName)) {
                        newInfo.setTitle(parser.nextText());
                    } else if ("detail".equals(tagName)) {
                        newInfo.setDetail(parser.nextText());
                    } else if ("comment".equals(tagName)) {
                        newInfo.setComment(Integer.valueOf(parser.nextText()));
                    } else if ("image".equals(tagName)) {
                        newInfo.setImageUrl(parser.nextText());
                    }
                    break;


                case XmlPullParser.END_TAG:
                    if ("new".equals(tagName)) {
                        newInfoList.add(newInfo);
                    }
                    break;
                default:
                    break;
            }
            eventType = parser.next();//取下一个事件类型
        }


      //  return newInfoList;

下面的xml文件来源于 传智播客

<?xml version="1.0" encoding="UTF-8" ?>
<news>
    <new>
        <title>3Q大战宣判: 腾讯获赔500万</title>
        <detail>最高法驳回360上诉, 维持一审宣判.</detail>
        <comment>6427</comment>
        <image>http://10.10.39.11:8080/Androiddata/images/1.jpg</image>
    </new>
    <new>
        <title>今日之声:北大雕塑被戴口罩</title>
        <detail>市民: 因雾霾起诉环保局; 公务员谈"紧日子": 坚决不出去.</detail>
        <comment>681</comment>
        <image>http://10.10.39.11:8080/Androiddata/images/2.jpg</image>
    </new>



</news
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值