当前位置:首页 > 开发 > 编程语言 > XML > 正文

android readXml SAX解析XML

发表于: 2014-06-13   作者:baoyao   来源:转载   浏览次数:
摘要: MainActivity.java package com.readXml; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import j
MainActivity.java

 package com.readXml;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ListView listView;
	private List<Mp3> list = new ArrayList<Mp3>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		/*StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());*/

		listView = (ListView) findViewById(R.id.listView1);

		listView.setAdapter(new MyAdapter(this, getData()));

	}

	private List<Mp3> getData() {
		// TODO Auto-generated method stub
		MyThread thread = new MyThread(list);
		thread.start();
		return list;
	}

	class MyThread extends Thread {

		public MyThread(List<Mp3> list2) {
			super();
			list = list2;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				URL url = new URL("http://192.168.1.102:8080/WebXML/MyXml.xml");

				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				InputStream inputStream = connection.getInputStream();

				SAXParserFactory factory = SAXParserFactory.newInstance();
				SAXParser parser = factory.newSAXParser();
				XMLReader reader = parser.getXMLReader();

				reader.setContentHandler(new MyHandler(list));
				reader.parse(new InputSource(inputStream));

				for (Mp3 mp3 : list) {
					System.out.println(mp3.getName() + "------" + mp3.getPath()
							+ "-------" + mp3.getSize());
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}
}



MyHandler.java


package com.readXml;

import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyHandler extends DefaultHandler {

	private List<Mp3> list;

	private Mp3 mp3;

	private String biaoqian;

	public MyHandler(List<Mp3> list) {
		super();
		this.list = list;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		biaoqian = qName;
		if ("mp3".equals(qName))
			mp3 = new Mp3();
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub

		biaoqian = "";
		if ("mp3".equals(qName))
			list.add(mp3);
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		String str = new String(ch, start, length);
		if ("name".equals(biaoqian))
			mp3.setName(str);
		if ("path".equals(biaoqian))
			mp3.setPath(str);
		if ("size".equals(biaoqian))
			mp3.setSize(str);

	}

}



Mp3.java
package com.readXml;

public class Mp3 {

	private String name;
	private String path;
	private String size;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

}


MyAdapter.java
package com.readXml;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

	private Context context;

	private List<Mp3> list;

	public MyAdapter(Context context, List<Mp3> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.item, null);
		TextView txt1 = (TextView) view.findViewById(R.id.textView1);
		TextView txt2 = (TextView) view.findViewById(R.id.textView2);
		TextView txt3 = (TextView) view.findViewById(R.id.textView3);

		txt1.setText(list.get(position).getName());
		txt2.setText(list.get(position).getPath());
		txt3.setText(list.get(position).getSize());

		return view;
	}

}



activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include layout="@layout/item" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>


item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="名字" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:text="路径" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:text="大小" />

</LinearLayout>

android readXml SAX解析XML

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
解析XML的方式有很多种,大家比较熟悉的可能就是DOM解析。 DOM(文件对象模型)解析:解析器读入整
SAX(Simple API for XML)是一个解析速度快并且占用内存少的xml解析器,非常适合用于Android等移动设
SAX(Simple API for XML)是一个解析速度快并且占用内存少的xml解析器,非常适合用于Android等移动设
解析XML的方式有很多种,大家比较熟悉的可能就是DOM解析。 DOM(文件对象模型)解析:解析器读入整
XML文件解析 XML:可扩展标记语言。 有DOM和SAX两种解析方式,下面介绍SAX解析方式 SAX: 一、什么是
  dom解析占用内存大(我这边需要解析各种各样的kml文件,有时4-5M的kml文件使用dom解析很多手机
---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
本篇随笔将详细讲解如何在Android当中解析服务器端传过来的XML数据,这里将会介绍解析xml数据格式的
在Android中解析XML文主要有三种方式,分别为Simple API for XML(SAX)、Document Object Model(DOM)
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号