android XmlPullParser的简单应用

package lxy.litsoft;

import java.io.IOException;
import java.io.StringReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class AppMain extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
			testParser();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
    private void testParser()throws XmlPullParserException,IOException
    {
    	/**XmlPullParserFactory: this class is used to create implementation of XML Pull Parser.**/
    	//XmlPullParserFactory这个类用于实现一个XML的解析器
    	/**newInstance(): Create a new instance of a PullParserFactory that can be used to create XML pull parsers .**/
    	//newInstance()方法用于实例化一个PullParserFactory对象,以便于创建XML解析器
    	XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    	
    	/**setNamespaceAware():Specifies that the parser produced by this factory will provide support for XML names paces.**/
    	//指定由XmlPullParserFactory对象产生的解析器能提供对XML命名空间的支持
        factory.setNamespaceAware(true);
        
    	/**Creates a new instance of a XML Pull Parser using the currently configured factory features.**/
    	//用当前的XmlPullParserFactory实例化一个XmlPullParser对象
        XmlPullParser xpp = factory.newPullParser();

        /**Set the input source for parser to the given reader and resets the parser.**/
        //为解析器输入源,源为一个StringReader对象
        /**StringReader:A specialized Reader that reads characters from a String in a sequential manner. **/
        //StringReader类是一个专门的阅读器,以顺序的方式从字符串里读取字符
        xpp.setInput( new StringReader ( "<liu>I am Liuliu</liu><huo>Missing Huohuo</huo>" ) );
        
        /**Returns the type of the current event (START_TAG, END_TAG, TEXT, etc.)**/
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
	         if(eventType == XmlPullParser.START_DOCUMENT) {
	        	 Log.d("test", "Start document");
	         } else if(eventType == XmlPullParser.END_DOCUMENT) {
	        	 Log.d("test", "End document");
	         } else if(eventType == XmlPullParser.START_TAG) {
	        	 Log.d("test", "Start tag: "+xpp.getName());
	         } else if(eventType == XmlPullParser.END_TAG) {
	        	 Log.d("test", "End tag: "+xpp.getName());
	         } else if(eventType == XmlPullParser.TEXT) {
	        	 Log.d("test", "Text: "+xpp.getText());
	         }
	         eventType = xpp.next();
        }
    }
}

输出结果:

Start document

Start tag: liu

Text:I am LIuliu

End tag: liu

Start tag: huo

Text: Missing Huohuo

End tag: huo

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值