In some applications, a little bit of data, directly stored in XML, is simple to implement,
1, the XML file into the asset folder, structure such as:
<?xml version= "1.0" encoding= "UTF-8"?>
<messages><message id= "1" ><title>3 monthly Diploma Digital Image Collection </title><date1>3</date1>< Content>3 month 12th around the year a diploma Digital Image collection, by the Xinhua News agency responsible for </content><url>23</url></message><message id= "2 "><title>3, September Curriculum registration </title><date1>3</date1><content>3 Month 15th, September 15 This semester curriculum registration, textbook subscription, Including course completion of student thesis registration </content><url>23</url>
</message>
</messages>
2. Construct the message class and parse the XML class Pullparseservice
3, call in activity.
The specific code is as follows:
1. Parsing XML classes
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 <message>getmessages (InputStream inputstream) throws exception{
arraylist<message> 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<message> ();//
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 per 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 the XML data into the application
Assetmanager asset = Getassets ();
try {
InputStream Input=asset.open ("Message.xml");
list<message> list = null;
try {
List = Pullparseservice.getmessages (input);
String tag= "Xmllllll";
int id = (int) (Math.random () *12);
LOG.I (tag, list.get (ID). getcontent ());
Publish 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 ();
}