slider

还是菜鸟
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Android之SAX解析Xml文件实现

Posted on 2012-02-13 15:27  slider  阅读(590)  评论(0编辑  收藏  举报
public class JokeBean {
private String id;
private String title;
private StringBuilder text=new StringBuilder();
public JokeBean(){
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getText() {
return text.toString();
}

public void setText(String text) {
this.text.append(text);
}
}

然后是解析类,继承DefaultHandler类:

public class SAXHandler extends DefaultHandler{
private static final String TAG = "SAXHandler";
private List<JokeBean> data;
private JokeBean joke;
private String preTAG;

private static final String JOKE="joke";
private static final String JOKE_ID="id";
private static final String JOKE_TITLE="title";
private static final String JOKE_TEXT="text";

public SAXHandler(List<JokeBean> data){
super();
this.data = data;
}

@Override
public void startDocument() throws SAXException {
if(data==null)
data=new ArrayList<JokeBean>();
Log.d(TAG, "startDocument ------- :");
super.startDocument();
}
@Override
public void endDocument() throws SAXException {
Log.d(TAG, "endDocument ------- :");
super.endDocument();
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
Log.d(TAG, "endDocument ------- :localName "+localName);

preTAG = localName;
if(JOKE.equals(localName)){
joke=new JokeBean();
}

super.startElement(uri, localName, qName, attributes);
}

@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
preTAG = "";
if(JOKE.equals(localName)){
data.add(joke);
Log.d(TAG, "endElement ------- :一个解析对象完成 。。。。 ");
}
super.endElement(uri, localName, qName);
}

@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String info = new String(ch,start,length);
Log.d(TAG, "characters ------- : info" +info);
if(preTAG.equals(JOKE_ID)){
joke.setId(info);
}else if(preTAG.equals(JOKE_TITLE)){
joke.setTitle(info);
}else if(preTAG.equals(JOKE_TEXT)){
joke.setText(info);
}
super.characters(ch, start, length);
}

public List<JokeBean> getdata(){
return data;
}
}

封装解析xml的工具类,方便客户端调用:

public class XMLUtil {

public static List<JokeBean> parserXML(Context context,int rawId){
List<JokeBean> data = new ArrayList<JokeBean>();
SAXParserFactory factory=SAXParserFactory.newInstance();
XMLReader reader;
try {
reader = factory.newSAXParser().getXMLReader();
reader.setContentHandler(new SAXHandler(data));
InputSource is = new InputSource(context.getResources().openRawResource(rawId));
is.setEncoding("utf-8");
reader.parse(is);
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
}

客户端主类代码:

public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button btnSAX, btnOutput;
EditText memo;
private List<JokeBean> data;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
memo = (EditText)findViewById(R.id.EditText01);
findViewById(R.id.btnOutput).setOnClickListener(this);
}

public void onClick(View v) {
if(v.getId()==R.id.btnOutput){
mHandler.post(new Runnable() {
public void run() {
data=XMLUtil.parserXML(MainActivity.this,R.raw.joke2);
memo.setText(data.get(0).getText());
}
});
}
}
private Handler mHandler = new Handler();
}

xml的格式:

<?xml version="1.0" encoding="UTF-8" ?> 
<jokes>
<joke>
<id>1</id>
<title>笑话一</title>
<text><![CDATA[我爱人民]]></text>
</joke>
</jokes>

参考:

http://blog.csdn.net/boyupeng/article/details/6304827