//sax解析的帮助类
package com.bai.saxxml;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.bai.saxxml.bean.Book;
public class SaxHander extends DefaultHandler {
private SAXParser parser;
private List<Book> bookList;
private Book book;
private String type;
private SAXParser parser;
private List<Book> bookList;
private Book book;
private String type;
public List<Book> handler(InputStream f, SaxHander aa) {
SAXParserFactory sax = SAXParserFactory.newInstance();
try {
parser = sax.newSAXParser();
parser.parse(f, aa);
SAXParserFactory sax = SAXParserFactory.newInstance();
try {
parser = sax.newSAXParser();
parser.parse(f, aa);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bookList;
}
// TODO Auto-generated catch block
e.printStackTrace();
}
return bookList;
}
@Override
public void startDocument() throws SAXException {
bookList = new ArrayList<Book>();
// TODO Auto-generated method stub
super.startDocument();
}
public void startDocument() throws SAXException {
bookList = new ArrayList<Book>();
// TODO Auto-generated method stub
super.startDocument();
}
// 开始元素解析
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if ("Book".equals(qName)) {
book = new Book();
}
type = qName;
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if ("Book".equals(qName)) {
book = new Book();
}
type = qName;
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
// 结束元素解析
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if ("Book".equals(qName)) {
bookList.add(book);
}
type = null;
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if ("Book".equals(qName)) {
bookList.add(book);
}
type = null;
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String data = new String(ch, start, length);
if ("name".equals(type)) {
book.setName(data);
} else if ("price".equals(type)) {
book.setJiage(data);
} else if ("author".equals(type)) {
book.setAuthor(data);
}
// TODO Auto-generated method stub
super.characters(ch, start, length);
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
String data = new String(ch, start, length);
if ("name".equals(type)) {
book.setName(data);
} else if ("price".equals(type)) {
book.setJiage(data);
} else if ("author".equals(type)) {
book.setAuthor(data);
}
// TODO Auto-generated method stub
super.characters(ch, start, length);
}
}
主类中的写法
package com.bai.saxxml;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.io.InputStream;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.bai.saxxml.bean.Book;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
}
public void butt(View v) {
try {
SaxHander sax = new SaxHander();
InputStream f = getAssets().open("Books.xml");
List<Book> list = sax.handler(f, sax);
tv.setText(list.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
SaxHander sax = new SaxHander();
InputStream f = getAssets().open("Books.xml");
List<Book> list = sax.handler(f, sax);
tv.setText(list.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}