xml解析,基于XmlPullParser

解析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

你可能感兴趣的:(android)