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);
}
}
Then the parsing class inherits the defaulthandler class:
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 -------: A parsing object is completed .... ");
}
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;
}
}
Encapsulate the tool class for parsing XML to facilitate client calls:
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;
}
}
Client main class code:
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 format:
<? XML version = "1.0" encoding = "UTF-8"?>
<Jokes>
<Joke>
<ID> 1 </ID>
<Title> joke 1 </title>
<Text> <! [CDATA [I love the people]> </text>
</Joke>
</Jokes>
Refer:
Http://blog.csdn.net/boyupeng/article/details/6304827