获取assets下资源文件并解析

获取assets下资源文件:

		try {
			AssetManager assetManager = mContext.getAssets();
			cates = ParseXML.parseCategory(assetManager.open("category.data",
					AssetManager.ACCESS_STREAMING));
		} catch (IOException e) {
			e.printStackTrace();
		}


ParseXML解析类:

package com.guozi.appstore.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.protocol.HTTP;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;


import com.guozi.appstore.bean.CategoryEntity;


public class ParseXML {
	
	public static List<CategoryEntity> parseCategory(InputStream inStream) {
		List<CategoryEntity> cates = null;
		CategoryEntity cate = null;
		XmlPullParser parser = getXmlParser(inStream);
		Log.i("Dust", "parser = " + parser);
		if (parser == null) {
			return cates;
		}
		cates = new ArrayList<CategoryEntity>();
		try {
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_TAG:
					if ("foobar".equals(parser.getName())) {
						cate = new CategoryEntity();
					} else if("categorytype".equals(parser.getName())){
						cate.setCategory(parser.nextText());
					} else if ("name".equals(parser.getName())) {
						cate.setName(parser.nextText());
					} else if ("url".equals(parser.getName())) {
						cate.setUrl(parser.nextText());
					} else if ("src".equals(parser.getName())) {
						cate.setLogo(parser.nextText());
					}
					break;

				case XmlPullParser.END_TAG:
					if ("foobar".equals(parser.getName())) {
						cates.add(cate);
					}
					break;
				}
				event = parser.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cates;
	}
	
	/**
	 * 获取Pull解析器
	 * 
	 * @param inputStream
	 * @return
	 */
	public static XmlPullParser getXmlParser(InputStream inputStream) {
		XmlPullParserFactory factory;
		XmlPullParser xmlParser = null;
		if (inputStream == null) {
			return xmlParser;
		}
		try {
			factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			xmlParser = factory.newPullParser();
			xmlParser.setInput(inputStream, HTTP.UTF_8);
		} catch (XmlPullParserException e1) {
			e1.printStackTrace();
		}
		factory = null;
		return xmlParser;
	}

/*	*//**
	 * 解析类别信息
	 * 
	 * @param inStream
	 * @return
	 *//*
	public static List<CategoryInfo> parseCategory(InputStream inStream) {
		List<CategoryInfo> cates = null;
		CategoryInfo cate = null;
		XmlPullParser parser = getXmlParser(inStream);
		Log.i("Dust", "parser = " + parser);
		if (parser == null) {
			return cates;
		}
		cates = new ArrayList<CategoryInfo>();
		try {
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_TAG:
					if ("foobar".equals(parser.getName())) {
						cate = new CategoryInfo();
					} else if("categorytype".equals(parser.getName())){
						cate.setCategoryType(parser.nextText());
					} else if ("name".equals(parser.getName())) {
						cate.setCategoryName(parser.nextText());
					} else if ("url".equals(parser.getName())) {
						cate.setCategoryUrl(parser.nextText());
					} else if ("src".equals(parser.getName())) {
						cate.setCategoryTypeBg(parser.nextText());
					}
					break;

				case XmlPullParser.END_TAG:
					if ("foobar".equals(parser.getName())) {
						cates.add(cate);
					}
					break;
				}
				event = parser.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cates;
	}


	*//**
	 * 解析子分类页面信息和应用列表
	 * 
	 * @param inStream
	 * @return
	 *//*
	public static Object[] parseSubCategoryFirstPageAppinfos(
			InputStream inStream) {
		if(inStream == null) return null;
		Object[] objsArr = null;
		PageInfo pageInfo = null;
		AppInfo appInfo = null;
		List<AppInfo> appInfos = null;
		XmlPullParser parser = getXmlParser(inStream);
		try {
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_TAG:
					if ("INFO".equalsIgnoreCase(parser.getName())) {
						pageInfo = new PageInfo();
						objsArr = new Object[2];
					} else if ("COUNT".equalsIgnoreCase(parser.getName())) {
						pageInfo.setPageItemTotal(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("PAGECOUNT".equalsIgnoreCase(parser.getName())) {
						pageInfo.setPageItemNum(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("PAGE".equalsIgnoreCase(parser.getName())) {
						pageInfo.setPageCurNum(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("PAGESIZE".equalsIgnoreCase(parser.getName())) {
						pageInfo.setPageTotal(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("ROWS".equalsIgnoreCase(parser.getName())) {
						appInfos = new ArrayList<AppInfo>();
					} else if ("foobar".equalsIgnoreCase(parser.getName())) {
						appInfo = new AppInfo();
					} else if ("id".equalsIgnoreCase(parser.getName())) {
						appInfo.setAppId(parser.nextText());
					} else if ("name".equalsIgnoreCase(parser.getName())) {
						appInfo.setAppName(parser.nextText());
					} else if ("ming".equalsIgnoreCase(parser.getName())) {
						appInfo.setAppIconUrl(parser.nextText());
					} else if (parser.getName().equalsIgnoreCase("score")) {
						appInfo.setAppScore(Utils.parserStrToFloat(parser
								.nextText()));
					} else if (parser.getName().equalsIgnoreCase("price")) {
						appInfo.setAppPrice(Utils.parserStrToFloat(parser
								.nextText()));
					} else if (parser.getName().equalsIgnoreCase("type")) {
						appInfo.setAppType(Utils.parserStrToInt(parser.nextText()));
					} else if (parser.getName().equalsIgnoreCase("bb")) {
						appInfo.setAppVersionName(parser.nextText());
					} else if (parser.getName().equalsIgnoreCase("packageName")) {
						appInfo.setAppPackageName(parser.nextText());
					} else if (parser.getName().equalsIgnoreCase("versioncode")) {
						appInfo.setAppVersionCode(Utils.parserStrToInt(parser
								.nextText()));
					} else if (parser.getName().equalsIgnoreCase("size")) {
						appInfo.setAppSize(parser.nextText());
					} else if (parser.getName().equalsIgnoreCase("updatetime")) {
						appInfo.setAppUpdateTime(parser.nextText());
					} else if (parser.getName().equalsIgnoreCase(
							"downloadCount")) {
						appInfo.setAppDownloadNum(Utils.parserStrToInt(parser
								.nextText()));
					} else if (parser.getName().equalsIgnoreCase("url")) {
						appInfo.setAppDetailPageUrl(parser.nextText());
					}
					break;

				case XmlPullParser.END_TAG:
					if ("INFO".equalsIgnoreCase(parser.getName())) {
						objsArr[0] = pageInfo;
					} else if ("foobar".equalsIgnoreCase(parser.getName())) {
						appInfos.add(appInfo);
					} else if ("ROWS".equalsIgnoreCase(parser.getName())) {
						objsArr[1] = appInfos;
					}
					break;
				}
				event = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return objsArr;
	}

	public static Object[] parseSpecialProjectApps(InputStream inStream) {
		Object[] objs = null;
		PageInfo pageInfo = null;
		List<AppEntity> appInfos = null;
		AppEntity appInfo = null;
		try {
			XmlPullParser parser = getXmlParser(inStream);
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_TAG:
					if ("data".equalsIgnoreCase(parser.getName())) {
						objs = new Object[2];
					} else if ("INFO".equalsIgnoreCase(parser.getName())) {
						pageInfo = new PageInfo();
					} else if ("COUNT".equalsIgnoreCase(parser.getName())) {
						pageInfo.setPageItemTotal(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("PAGECOUNT".equalsIgnoreCase(parser.getName())) {
						pageInfo.setPageItemNum(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("PAGE".equalsIgnoreCase(parser.getName())) {
						pageInfo.setPageCurNum(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("PAGESIZE".equalsIgnoreCase(parser.getName())) {
						pageInfo.setPageTotal(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("pagegroupname".equalsIgnoreCase(parser
							.getName())) {
						pageInfo.setPageType(parser.nextText());
					} else if ("ROWS".equalsIgnoreCase(parser.getName())) {
						appInfos = new ArrayList<AppEntity>();
					} else if ("foobar".equalsIgnoreCase(parser.getName())) {
						appInfo = new AppEntity();
					} else if ("id".equalsIgnoreCase(parser.getName())) {
						//appInfo.setAppId(parser.nextText());
						appInfo.setId(Utils.parserStrToLong(parser.nextText()));
					} else if ("name".equalsIgnoreCase(parser.getName())) {
						appInfo.setName(parser.nextText());
					} else if ("ming".equalsIgnoreCase(parser.getName())) {
						//appInfo.setAppIconUrl(parser.nextText());
						appInfo.setLogo(parser.nextText());
					} else if ("score".equalsIgnoreCase(parser.getName())) {
						//appInfo.setAppScore(Utils.parserStrToFloat(parser.nextText()));
						appInfo.setScore(Utils.parserStrToFloat(parser.nextText()));
					} else if ("price".equalsIgnoreCase(parser.getName())) {
						//appInfo.setAppPrice(Utils.parserStrToFloat(parser.nextText()));
						appInfo.setPrice(Utils.parserStrToFloat(parser.nextText()));
					} else if ("type".equalsIgnoreCase(parser.getName())) {
						//appInfo.setAppType(Utils.parserStrToInt(parser.nextText()));
						appInfo.setType(Utils.parserStrToInt(parser.nextText()));
					} else if ("bb".equalsIgnoreCase(parser.getName())) {
						appInfo.setVersionName(parser.nextText());
					} else if ("packageName".equalsIgnoreCase(parser.getName())) {
						appInfo.setPackageName(parser.nextText());
					} else if ("versioncode".equalsIgnoreCase(parser.getName())) {
						appInfo.setVersionCode(Utils.parserStrToInt(parser
								.nextText()));
					} else if ("size".equalsIgnoreCase(parser.getName())) {
						appInfo.setSize(parser.nextText());
					} else if ("updatetime".equalsIgnoreCase(parser.getName())) {
						//appInfo.setAppUpdateTime(parser.nextText());
						appInfo.setUpdateTime(Utils.parserStrToLong(parser.nextText()));
					} else if ("downloadCount".equalsIgnoreCase(parser
							.getName())) {
						//appInfo.setAppDownloadNum(Utils.parserStrToInt(parser.nextText()));
						appInfo.setDownloadTimes(Utils.parserStrToLong(parser.nextText()));
					} else if ("url".equalsIgnoreCase(parser.getName())) {
						//appInfo.setAppDownloadUrl(parser.nextText());
						appInfo.setUrl(parser.nextText());
					}
					break;

				case XmlPullParser.END_TAG:
					if ("INFO".equalsIgnoreCase(parser.getName())) {
						objs[0] = pageInfo;
					} else if ("foobar".equalsIgnoreCase(parser.getName())) {
						appInfos.add(appInfo);
					} else if ("ROWS".equalsIgnoreCase(parser.getName())) {
						objs[1] = appInfos;
					}
					break;
				}
				event = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return objs;
	}

	public static List<CategoryInfo> parseSpecialProjects(InputStream inStream) {
		List<CategoryInfo> cates = null;
		CategoryInfo cate = null;
		try {
			XmlPullParser parser = getXmlParser(inStream);
			int event = parser.getEventType();
			while (event != XmlPullParser.END_DOCUMENT) {
				switch (event) {
				case XmlPullParser.START_TAG:
					if ("data".equalsIgnoreCase(parser.getName())) {
						cates = new ArrayList<CategoryInfo>();
					} else if ("foobar".equalsIgnoreCase(parser.getName())) {
						cate = new CategoryInfo();
					} else if ("name".equalsIgnoreCase(parser.getName())) {
						cate.setCategoryName(parser.nextText());
					} else if ("url".equalsIgnoreCase(parser.getName())) {
						cate.setCategoryUrl(parser.nextText());
					} else if ("img".equalsIgnoreCase(parser.getName())) {
						cate.setCategoryTypeBg(parser.nextText());
					}
					break;

				case XmlPullParser.END_TAG:
					if ("foobar".equalsIgnoreCase(parser.getName())) {
						cates.add(cate);
					}
					break;
				}
				event = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return cates;
	}

	public static final Object[] getAppInfos(InputStream inputStream) {
		Object[] objects = null;
		PageInfo pageInfo = null;
		ArrayList<AppInfo> appInfoArray = null;
		XmlPullParser parser = null;
		parser = getXmlParser(inputStream);
		if (parser == null) {
			return objects;
		}

		try {
			int eventType = parser.getEventType();
			AppInfo appInfo = null;
			String name = null;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				name = null;
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					name = parser.getName();
					if (name.equalsIgnoreCase("data")) {
						objects = new Object[2];
					} else if ((name.equalsIgnoreCase("INFO"))
							&& pageInfo == null) {
						pageInfo = new PageInfo();
					} else if (name.equalsIgnoreCase("key")) {
					} else if (name.equalsIgnoreCase("COUNT")) {
						pageInfo.setPageCurItemNum(Utils
								.parserStrToInt(parser.nextText()));
					} else if (name.equalsIgnoreCase("PAGECOUNT")) {
						pageInfo.setPageItemNum(Utils.parserStrToInt(parser
								.nextText()));
					} else if (name.equalsIgnoreCase("PAGE")) {
						pageInfo.setPageCurNum(Utils.parserStrToInt(parser
								.nextText()));
					} else if (name.equalsIgnoreCase("PAGESIZE")) {
						pageInfo.setPageTotal(Utils.parserStrToInt(parser
								.nextText()));
					} else if (name.equalsIgnoreCase("ROWS")) {
						appInfoArray = new ArrayList<AppInfo>(20);
					} else if (name.equalsIgnoreCase("foobar")) {
						appInfo = new AppInfo();
					} else if (name.equalsIgnoreCase("id")) {
						appInfo.setAppId(parser.nextText());
					} else if (name.equalsIgnoreCase("name")) {
						appInfo.setAppName(parser.nextText());
					} else if (name.equalsIgnoreCase("ming")) {
						appInfo.setAppIconUrl(parser.nextText());
					} else if (name.equalsIgnoreCase("score")) {
						appInfo.setAppScore(Utils.parserStrToFloat(parser
								.nextText()));
					} else if (name.equalsIgnoreCase("price")) {
						appInfo.setAppPrice(Utils.parserStrToFloat(parser
								.nextText()));
					} else if (name.equalsIgnoreCase("type")) {
						appInfo.setAppType(Utils.parserStrToInt(parser.nextText()));
					} else if ((name.equalsIgnoreCase("size"))) {
						appInfo.setAppSize(parser.nextText());
					} else if (name.equalsIgnoreCase("updatetime")) {
						appInfo.setAppUpdateTime(parser.nextText());
					} else if (name.equalsIgnoreCase("downloadCount")) {
						appInfo.setAppDownloadNum(Utils.parserStrToInt(parser
								.nextText()));
					} else if (name.equalsIgnoreCase("desc")) {
						appInfo.setAppDescription(parser.nextText());
					} else if (name.equalsIgnoreCase("url")) {
						appInfo.setAppDownloadUrl(parser.nextText());
					} else if (name.equalsIgnoreCase("bb")) {
						appInfo.setAppVersionName(parser.nextText());
					} else if (name.equalsIgnoreCase("sygj")) {
						appInfo.setAppRequired(parser.nextText());
					} else if (name.equalsIgnoreCase("kfz")) {
						appInfo.setAppOwn(parser.nextText());
					} else if (name.equalsIgnoreCase("versionCode")) {
						appInfo.setAppVersionCode(Utils.parserStrToInt(parser
								.nextText()));
					} else if (name.equalsIgnoreCase("packageName")) {
						appInfo.setAppPackageName(parser.nextText());
					}
					break;
				case XmlPullParser.END_TAG:
					if (parser.getName().equalsIgnoreCase("foobar")) {
						appInfoArray.add(appInfo);
						appInfo = null;
					} else if (parser.getName().equalsIgnoreCase("INFO")) {
						objects[0] = pageInfo;
						pageInfo = null;
					} else if (parser.getName().equalsIgnoreCase("ROWS")) {
						objects[1] = appInfoArray;
						appInfoArray = null;
					}
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
		return objects;
	}*/
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值