Android reads xml

Source: Internet
Author: User
Tags date1

In some applications, a small amount of data is directly stored in XML, which is easy to implement,

1. Put the xml file into the asset folder. The structure is as follows:


Graduation certificate Digital Photo acquisition in March 3 Around March 12, a digital graduation certificate was collected by Xinhua News Agency. 23 3. September course registration 3 Course registration and textbook subscription for the current semester in May and may, including registration of students' thesis after the course is completed 23

2. Construct the message class and parse the xml class PullParseService

3. Call in activity.

The Code is as follows:

1. parse xml class

Package com.edu. notice;

Import java. io. InputStream;
Import java. util. ArrayList;
Import org. xmlpull. v1.XmlPullParser;


Import android. util. Xml;

Public class PullParseService {
Public static ArrayList GetMessages (InputStream inputStream) throws Exception {
ArrayList Messages = null;
Message message = null;
XmlPullParser parser = Xml. newPullParser ();
Parser. setInput (inputStream, "UTF-8 ");
Int event = parser. getEventType ();//
While (event! = XmlPullParser. END_DOCUMENT ){
Switch (event ){
Case XmlPullParser. START_DOCUMENT ://
Messages = new ArrayList ();//
Break;
Case XmlPullParser. START_TAG ://
If ("message". equals (parser. getName ())){//
Message = new Message ();
Message. setId (Integer. parseInt (parser. getAttributeValue (0 )));//
}
If (message! = Null ){
If ("title". equals (parser. getName ())){//
Message. setTitle (parser. nextText ());
} Else if ("date1". equals (parser. getName ())){//
Message. setData1 (Integer. parseInt (parser. nextText ()));
} Else if ("content". equals (parser. getName ())){
Message. setContent (parser. nextText ());
} Else if ("url". equals (parser. getName ())){
Message. setUrl (parser. nextText ());
}
}
Break;
Case XmlPullParser. END_TAG ://
If ("message". equals (parser. getName ())){//
Messages. add (message );//
Message = null;
}
Break;
}
Event = parser. next ();//
} // End while
Return messages;

};
}

2. xml each object information class

Package com.edu. notice;

Public class Message {
Private int id;
Private String title;
Private int data1;
Private String content;
Private String url;
/**
* @ Return the id
*/
Public int getId (){
Return id;
}
/**
* @ Param id the id to set
*/
Public void setId (int id ){
This. id = id;
}
/**
* @ Return the title
*/
Public String getTitle (){
Return title;
}
/**
* @ Param title the title to set
*/
Public void setTitle (String title ){
This. title = title;
}
/**
* @ Return the data1
*/
Public int getData1 (){
Return data1;
}
/**
* @ Param data1 the data1 to set
*/
Public void setData1 (int data1 ){
This. data1 = data1;
}
/**
* @ Return the content
*/
Public String getContent (){
Return content;
}
/**
* @ Param content the content to set
*/
Public void setContent (String content ){
This. content = content;
}
/**
* @ Return the url
*/
Public String getUrl (){
Return url;
}
/**
* @ Param url the url to set
*/
Public void setUrl (String url ){
This. url = url;
}



}

3. Read xml data and apply it to the application.

AssetManager asset = getAssets ();
Try {
InputStream input = asset. open ("message. xml ");
List List = null;
Try {
List = PullParseService. getMessages (input );

String tag = "xmllllll ";
Int id = (int) (Math. random () * 12 );
Log. I (tag, list. get (id). getContent ());
// Publish the content to broadcast
SetReminder (true, list. get (id). getContent ());


} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

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.