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 Listlist = new ArrayList (); @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 getData() { // TODO Auto-generated method stub MyThread thread = new MyThread(list); thread.start(); return list; } class MyThread extends Thread { public MyThread(List 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 Listlist; private Mp3 mp3; private String biaoqian; public MyHandler(List 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 Listlist; public MyAdapter(Context context, List 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
item.xml