XmlResourceParser解析XML文件

前端之家收集整理的这篇文章主要介绍了XmlResourceParser解析XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

官方文档:

The XML parsing interface returned for an XML resource. This is a standard XmlPullParser interface,as well as an extended AttributeSet interface and an additional close() method on this interface for the client to indicate when it is done reading the resource.


解释:

XmlResourceParser是一个接口类,继承XmlPullParser和AttributeSet。它返回一个xml资源文件。这样说并不准确,应该是通过它可以访问xml中的资源。当结束使用后,可以调用其close方法。源码如下:

/**
 * The XML parsing interface returned for an XML resource.  This is a standard
 * XmlPullParser interface,as well as an extended AttributeSet interface and
 * an additional close() method on this interface for the client to indicate
 * when it is done reading the resource.
 */
public interface XmlResourceParser extends XmlPullParser,AttributeSet {
    /**
     * Close this interface to the resource.  Calls on the interface are no
     * longer value after this call.
     */
    public void close();
}

还有两个比较重要的标签:都是通过getEventType方法返回的。

START_TAG:开始读取xml文件标签

END_DOCUMENT:xml文件的结尾标签。如果getEventType()的结果是它,就说明读取完毕。


一样,拿一个例子说明:

1.创建一个xml文件(books.xml),用来解析:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<book price="20" date="2012.12.12">android</book>
	<book price="20" date="2011.11.11">java</book>
	<book price="20" date="2010.10.10">c/c++</book>
</resources>

布局文件main.xml中,包含一个Button和TextView,Button用来执行解析xml操作,TextView显示解析结果,即books.xml文件中的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="解析xml文件" />

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

主Activity代码
package com.cb.parsexml;

import java.io.IOException;

import org.crazyit.image.R;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ParseXml extends Activity {
	private TextView mTextView;
	private Button mParseXmlBtn;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.main);
		super.onCreate(savedInstanceState);

		mTextView = (TextView) findViewById(R.id.tv);
		mParseXmlBtn = (Button) findViewById(R.id.btn);
		mParseXmlBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//根据指定的xml文件,返回XmlResourceParser。通过它,可以访问对应的xml文件中的数据
				XmlResourceParser xrp = getResources().getXml(R.xml.books);
				try {
					//StringBuilder对象用来储存获取的xml信息
					StringBuilder sb = new StringBuilder("");
					while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { //是否读取到文件尾
						if (xrp.getEventType() == XmlResourceParser.START_TAG) { //是否是开始读取的标签
							//获取标签名称
							String tagName = xrp.getName();
							Log.i("cb","tagName is " + tagName);
							if (tagName.equals("book")) {
								//获取属性值
								String bookPrice = xrp.getAttributeValue(null,"price");
								sb.append("价格:" + bookPrice + ",");
								String bookDate = xrp.getAttributeValue(null,"date");
								sb.append("日期:" + bookDate + ",");
								String bookName = xrp.nextText();
								sb.append("书名:" + bookName);
							}
							sb.append("\n");
						}
						//下一条记录
						xrp.next();
					}
					mTextView.setText(sb.toString());

				} catch (XmlPullParserException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} 
			}
		});

	}
}

点击按钮后,效果如图:

原文链接:https://www.f2er.com/xml/300104.html

猜你在找的XML相关文章