Android SAX解析XML文件

SAXPersonService.java

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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

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

import cn.itcast.domain.Person;
/**
 * 采用SAX解析XML内容
 */
public class SAXPersonService {
	
	public List<Person> getPersons(InputStream inStream) throws Throwable{
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		PersonParser personParser = new PersonParser();
		parser.parse(inStream, personParser);
		inStream.close();
		return personParser.getPersons();
	}

	private final class PersonParser extends DefaultHandler{
		private List<Person> persons = null;
		private String tag = null;
		private Person person = null;

		public List<Person> getPersons() {
			return persons;
		}

		@Override
		public void startDocument() throws SAXException {
			persons = new ArrayList<Person>();
		}
		
		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			if("person".equals(localName)){
				person = new Person();
				person.setId(new Integer(attributes.getValue(0)));
			}
			tag = localName;
		}
		
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if(tag!=null){
				String data = new String(ch, start, length);//获取文本节点的数据
				if("name".equals(tag)){
					person.setName(data);
				}else if("age".equals(tag)){
					person.setAge(new Short(data));
				}
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if("person".equals(localName)){
				persons.add(person);
				person = null;
			}
			tag = null;
		}
	}
}

测试类PersonServiceTest.java

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import cn.itcast.domain.Person;
import cn.itcast.service.SAXPersonService;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;

public class PersonServiceTest extends AndroidTestCase {
	private static final String TAG = "PersonServiceTest";

	public void testSAXGetPersons() throws Throwable{
		SAXPersonService service = new SAXPersonService();
		InputStream inStream = getClass().getClassLoader().getResourceAsStream

("itcast.xml");
		List<Person> persons = service.getPersons(inStream);
		for(Person person : persons){
			Log.i(TAG, person.toString());    
		}
	}
	
}



 

你可能感兴趣的:(Android开发,Android开发,Sax解析XML)