android readXml SAX解析XML

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>

你可能感兴趣的:(xml,xml解析,saxXml)