码迷,mamicode.com
首页 > 其他好文 > 详细

XML解析

时间:2014-05-09 21:56:25      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   tar   ext   

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);  
				}





XML解析,布布扣,bubuko.com

XML解析

标签:style   blog   class   code   tar   ext   

原文地址:http://blog.csdn.net/hades0821/article/details/25422051

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!