Xml file to be parsed
<? Xml version = "1.0" encoding = "UTF-8"?> <Helps>
Package com. gfive. help; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. io. serializable; import java. util. arrayList; import java. util. list; import org. xmlpull. v1.XmlPullParser; import com. gfive. help. tools. tools; import android. content. context; import android. util. xml;/*** help class homepage object class * @ author Administrator */public class HelpBean implemen Ts Serializable {private static final long serialVersionUID = 1L; public static final String HELP = "help"; public static final String ID = "id "; public static final String NAME = "name"; public static final String PIC_PATH = "picPath"; public String id; // number public String name; // NAME public String type; // Image Type public String picPath; // picture private static final String INDEX_PATH = "/gfive/index/"; // picture on the navigation page /*** Pull parses xml file streams * @ param is * @ return */public static List <HelpBean> getHelps (InputStream is) {List <HelpBean> list = null; HelpBean help = null; xmlPullParser parser = Xml. newPullParser (); int event =-1; try {parser. setInput (is, "UTF-8"); event = parser. getEventType (); while (event! = XmlPullParser. END_DOCUMENT) {switch (event) {case XmlPullParser. START_DOCUMENT: list = new ArrayList <HelpBean> (); break; case XmlPullParser. START_TAG: if (HelpBean. HELP. equals (parser. getName () {// get the node name pointed to by the pointer help = new HelpBean ();} if (help! = Null) {if (HelpBean. ID. equals (parser. getName () {String id = parser. nextText (); help. id = id;} else if (HelpBean. NAME. equals (parser. getName () {String name = parser. nextText (); help. name = name;} else if (HelpBean. PIC_PATH.equals (parser. getName () {String picPath = parser. nextText (); help. picPath = picPath ;}} break; case XmlPullParser. END_TAG: if (HelpBean. HELP. equals (parser. getName () {list. add (El P); help = null;} break; default: break;} event = parser. next () ;}} catch (Exception e) {e. printStackTrace ();} return list;}/*** read files in the Assert directory * @ param context * @ return */public static List <HelpBean> readAssertXml (Context context) {List <HelpBean> lists = new ArrayList <HelpBean> (); InputStream is = null; try {is = context. getResources (). getAssets (). open ("help. xml "); lists. addAll (HelpBean. getHelps (is);} ca Tch (IOException e) {e. printStackTrace () ;}finally {try {if (null! = Is) is. close ();} catch (IOException e) {e. printStackTrace () ;}} return lists;}/*** read the file under the SD card * @ param context * @ return */public static List <HelpBean> readSDCardXml (Context context) {List <HelpBean> lists = new ArrayList <HelpBean> (); InputStream is = null; try {File file = new File (Tools. SDCARD_STORAGE_PATH + INDEX_PATH + "help. xml "); if (file. exists () {is = new FileInputStream (file); lists. addAll (HelpBean. ge THelps (is) ;}} catch (Exception e) {e. printStackTrace () ;}finally {try {if (null! = Is) is. close () ;}catch (IOException e) {e. printStackTrace () ;}} return lists ;}}
Save data
public static void save(List<Persion> list,OutputStream os) throws IllegalArgumentException, IllegalStateException, IOException{ XmlSerializer serializer = Xml.newSerializer(); serializer.setOutput(os, "UTF-8"); serializer.startDocument("UTF-8", true); serializer.startTag(null, "persons"); for(Persion persion : list){ serializer.startTag(null, "person"); serializer.attribute(null, "id", String.valueOf(persion.getId())); serializer.startTag(null, "name"); serializer.text(persion.getName()); serializer.endTag(null, "name"); serializer.startTag(null, "age"); serializer.text(String.valueOf(persion.getAge())); serializer.endTag(null, "age"); serializer.endTag(null, "person"); } serializer.endTag(null, "persons"); serializer.endDocument(); os.flush(); os.close(); } }