Android-based sax parsing XML file implementation

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.