首页 > 代码库 > XML解析

XML解析

XML解析方式

一、SAX解析

1、使用SAXParserFactory创建一个SAXParserFactory对象,通过SAXParserFactory对象得到SAXParser解析器
2、为SAXParser解析器设置一个Handle处理
3、SAXParser.parser开始解析

代码如下:
				InputStream is=getAssets().open("books.xml");
				SAXParserFactory saxFactory=SAXParserFactory.newInstance();
				SAXParser saxparser=saxFactory.newSAXParser();
				MyHandle myhandle=new MyHandle();
				saxparser.parse(is, myhandle);
				books=myhandle.getBooks();
				for(Book book:books){
					Log.i(TAG, book.toString());  
				}

MyHandle内容
public class MyHandle extends DefaultHandler{

	private List<Book> books;
	private StringBuilder stringbuilder;
	private Book book;
	
	public List<Book> getBooks(){
		System.out.println("getbooks");
		return books;
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		stringbuilder.append(ch, start, length);
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if(localName.equals("id")){
			book.setId(stringbuilder.toString());
		}
		else if(localName.equals("name")){
			book.setName(stringbuilder.toString());
		}
		else if(localName.equals("author")){
			book.setAuthor(stringbuilder.toString());
		}else if(localName.equals("book")){
			books.add(book);
		}
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		books=new ArrayList<Book>();
		stringbuilder=new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		
		if(localName.equals("book")){
			book=new Book();
		}
		stringbuilder.setLength(0);
	}

}

二、PULL解析方式
1、创建一个Parser接口,代码如下
public interface BookParser {

	public List<Book> parse(InputStream in)throws Exception;
	
	public String serialize(List<Book> books)throws Exception;
}
2、实现此接口
public class BookPullParse implements BookParser {

	
	
	
	@Override
	public List<Book> parse(InputStream in) throws Exception {
		// TODO Auto-generated method stub
		List<Book> books=null;
		Book book=null;
		XmlPullParser parser=Xml.newPullParser();
		parser.setInput(in,"UTF-8");
		int event=parser.getEventType();
		
		while(event!=parser.END_DOCUMENT){
			switch(event){
			case XmlPullParser.START_DOCUMENT:
				books=new ArrayList<Book>();
				break;
			
			case XmlPullParser.START_TAG:
				if(parser.getName().equals("book")){
					book=new Book();
				}
				else if(parser.getName().equals("id")){
					event=parser.next();
					book.setId(parser.getText());
				}
				else if(parser.getName().equals("name")){
					event=parser.next();
					book.setName(parser.getText());
				}
				else if(parser.getName().equals("author")){
					event=parser.next();
					book.setAuthor(parser.getText());
				}
				break;
			case XmlPullParser.END_TAG:
				if(parser.getName().equals("book")){
					books.add(book);
					book=null;
				}
				break;
			}
			event=parser.next();
		}
		return books;
	}

	@Override
	public String serialize(List<Book> books) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
3、调用parser解析
				InputStream in=getAssets().open("books.xml");
				BookParser parser=new BookPullParse();
				List<Book> books=parser.parse(in);
				for(Book book:books){
					System.out.println(book);  
				}