源地址:http://www.cnblogs.com/JerryWang1991/archive/2012/02/24/2365507.html
关于SAX的使用体会:
package com.example.li.demoforxml.XMLHelper;
import android.util.Log;
import com.example.li.demoforxml.Entity.channel;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
/**
* Created by li on 2016/3/15.
*/
public class SAXPraserHelper extends DefaultHandler{
final static private String TAG="Demo";
int currentState =0;
channel chann;
List<channel> list;
public List<channel> getList() {
return list;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
Log.v(TAG,"charactersbegin");
String theString = String.valueOf(ch,start,length);
Log.v(TAG,"--->theString-->"+theString+"--->"+currentState+"<---");
if(currentState != 0){
Log.v(TAG,"set"+theString);
chann.setName(theString);
currentState = 0;
}
Log.v(TAG,"characterend");
return;
}
/**
*文档解析开始
*/
@Override
public void startDocument() throws SAXException {
Log.v(TAG,"startDocumentbegin");
list = new ArrayList<channel>();
Log.v(TAG,"startDocumentend");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
Log.v(TAG,"starElementbegin");
chann = new channel();
if(localName.equals("item")){
for(int i=0; i<attributes.getLength(); i++){
if(attributes.getLocalName(i).equals("id")){
chann.setId(attributes.getValue(i));
}else if(attributes.getLocalName(i).equals("url")){
chann.setUrl(attributes.getValue(i));
}
}
currentState = 1;
Log.v(TAG,"-->"+currentState);
Log.v(TAG,"startElementend---if");
//return用来终止,防止currentState重新又变成0
return;
}
Log.v(TAG,"startElementend");
currentState = 0;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
Log.v(TAG, "endElementstart");
if(localName.equals("item")) {
list.add(chann);
Log.v(TAG, "endElementend---if");
}
Log.v(TAG,"endElementend");
}
/**
* 文档解析结束
* @throws SAXException
*/
@Override
public void endDocument() throws SAXException {
Log.v(TAG,"endDocumentstart");
super.endDocument();
Log.v(TAG, "endDocumentend");
}
}
03-15 23:42:15.200 4588-4588/com.example.li.demoforxml V/Demo: startDocumentbegin
03-15 23:42:15.200 4588-4588/com.example.li.demoforxml V/Demo: startDocumentend
03-15 23:42:15.201 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.205 4588-4588/com.example.li.demoforxml V/Demo: startElementend
03-15 23:42:15.205 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: --->theString--> --->0<---
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.206 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: -->1
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: startElementend---if
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->百度--->1<---
03-15 23:42:15.208 4588-4588/com.example.li.demoforxml V/Demo: set百度
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: endElementend---if
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.210 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString--> --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: -->1
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: startElementend---if
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->腾讯--->1<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: set腾讯
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementend---if
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString--> --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: -->1
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: startElementend---if
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->新浪--->1<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: set新浪
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementend---if
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: --->theString--> --->0<---
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.211 4588-4588/com.example.li.demoforxml V/Demo: starElementbegin
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: -->1
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: startElementend---if
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->淘宝--->1<---
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: set淘宝
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementend---if
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: charactersbegin
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: --->theString-->
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: --->0<---
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: characterend
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementstart
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endElementend
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endDocumentstart
03-15 23:42:15.212 4588-4588/com.example.li.demoforxml V/Demo: endDocumentend
日志文件显示除了最后一个循环,都是调用了两次characterend,不懂这个