需要解析的xml文件
<?xml version="1.0" encoding="utf-8"?> <helps> <help> <id>1</id> <name>动物</name> <type>1</type> <picPath>/gfive/index/index_image1.png</picPath> </help> <help> <id>2</id> <name>植物</name> <type>2</type> <picPath>/gfive/index/index_image2.png</picPath> </help> <help> <id>3</id> <name>人物</name> <type>3</type> <picPath>/gfive/index/index_image3.png</picPath> </help> </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; /** * 帮助类首页实体类 * @author Administrator */ public class HelpBean implements 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; //编号 public String name; //名字 public String type; //图片类型 public String picPath; //图片 private static final String INDEX_PATH = "/gfive/index/"; //导航页的图片 /** * pull 解析xml文件流 * @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())) {// 得到指针所指向的节点名称 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(help); help = null; } break; default: break; } event = parser.next(); } } catch (Exception e) { e.printStackTrace(); } return list; } /** * 读取Assert目录下的文件 * @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)); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(null != is) is.close(); } catch (IOException e) { e.printStackTrace(); } } return lists; } /** * 读取SD卡下的文件 * @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.getHelps(is)); } } catch (Exception e) { e.printStackTrace(); }finally{ try { if(null != is) is.close(); } catch (IOException e) { e.printStackTrace(); } } return lists; } }
保存数据
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(); } }