public class SaxGiftHandler extends DefaultHandler { private String Tag="SaxGiftHandler"; //XML Tag private String tRootElement="gift"; private String tEntryElement="entry"; private static final String ATOM_NAMESPACE="http://www.w3.org/2005/Atom"; //XML result private ArrayList<GiftInfo> giftInfoList0,giftInfoList1,giftInfoList2,giftInfoList3; private GiftInfo giftInfo; //resolve XML public void parse(InputStream inStream){ final String outPath=RoomCommonData.outPath; giftInfoList0=new ArrayList<GiftInfo>(); giftInfoList1=new ArrayList<GiftInfo>(); giftInfoList2=new ArrayList<GiftInfo>(); giftInfoList3=new ArrayList<GiftInfo>(); RootElement root=new RootElement(ATOM_NAMESPACE,tRootElement); Element entry=root.getChild(ATOM_NAMESPACE,tEntryElement); //entry tag entry.setStartElementListener(new StartElementListener(){ public void start(Attributes attributes) { // TODO Auto-generated method stub giftInfo=new GiftInfo(); String id=attributes.getValue("id"); String name=attributes.getValue("name"); String price=attributes.getValue("price"); String num=attributes.getValue("num"); String type=attributes.getValue("type"); giftInfo.setGiftId(Integer.parseInt(id)); giftInfo.setGiftName(name); giftInfo.setGiftPrice(Integer.parseInt(price)); giftInfo.setGiftCnt(Integer.parseInt(num)); giftInfo.setGiftType(type); //从/data/data解析 //giftInfo.setGiftPath(outPath+File.separator+"gift"+File.separator+id+".png"); giftInfo.setGiftPath("gift"+File.separator+id+".png"); } }); entry.setEndElementListener(new EndElementListener(){ public void end() { // TODO Auto-generated method stub Integer typeId=Integer.parseInt(giftInfo.getGiftType()); switch(typeId){ case 0: giftInfoList0.add(giftInfo); break; case 1: giftInfoList1.add(giftInfo); break; case 2: giftInfoList2.add(giftInfo); break; case 3: giftInfoList3.add(giftInfo); break; default: return; } } }); try{ Xml.parse(inStream, Xml.Encoding.UTF_8,root.getContentHandler()); }catch(Exception e){ e.printStackTrace(); } } public List<GiftInfo> getGiftInfoList0(){ return this.giftInfoList0; } public List<GiftInfo> getGiftInfoList1(){ return this.giftInfoList1; } public List<GiftInfo> getGiftInfoList2(){ return this.giftInfoList2; } public List<GiftInfo> getGiftInfoList3(){ return this.giftInfoList3; } }
public class SaxGiftService { private SaxGiftHandler handler; private List<GiftInfo> gifts_hot,gifts_prize,gifts_else,gifts_store; private String giftPath; private Context context; public SaxGiftService(Context context,String giftPath){ this.context=context; this.giftPath=giftPath; } public void resolveGifts(){ InputStream inStream = null; try { inStream = context.getAssets().open(giftPath); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } handler=new SaxGiftHandler(); handler.parse(inStream); gifts_hot=handler.getGiftInfoList0(); gifts_prize=handler.getGiftInfoList1(); gifts_else=handler.getGiftInfoList2(); gifts_store=handler.getGiftInfoList3(); } public List<GiftInfo> getGiftHot(){ return this.gifts_hot; } public List<GiftInfo> getGiftPrize(){ return this.gifts_prize; } public List<GiftInfo> getGiftElse(){ return this.gifts_else; } public List<GiftInfo> getGiftIStore(){ return this.gifts_store; } }private void createGiftView() {
RoomCommonData.giftPath="...../assets/";
SaxGiftService sgs=new SaxGiftService(acti,RoomCommonData.giftPath+"gift.xml");
sgs.resolveGifts();
gifts_hot=sgs.getGiftHot();
gifts_prize=sgs.getGiftPrize();
gifts_else=sgs.getGiftElse();
gifts_store=sgs.getGiftIStore();
ChatEmotion.giftList=gifts_hot;
ChatEmotion.initAsycGiftImageLoader(acti);
}