下载、缓存、解析XML文件(Pull解析)

前端之家收集整理的这篇文章主要介绍了下载、缓存、解析XML文件(Pull解析)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

注意:URL地址可以自己换,解析节点可以换


	TextView show1;
	TextView show2;
	TextView show3;
	String text1 = "";
	String text2 = "";
	String text3 = "";
	String url = "http://witcard.net/image/config/android_version_iCard.xml";
	// URL地址
	static String dir = ".tanghongyu/";
	// 缓存存放的文件夹
	Context context;
	File file = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		context = getApplicationContext();
		show1 = (TextView) findViewById(R.id.show1);
		show2 = (TextView) findViewById(R.id.show2);
		show3 = (TextView) findViewById(R.id.show3);
		File cacheDir = null;
		if (url.indexOf("android_version_iCard") != -1) {
			if (android.os.Environment.getExternalStorageState().equals(
					android.os.Environment.MEDIA_MOUNTED)) {
				file = new File(
						android.os.Environment.getExternalStorageDirectory(),dir
								+ url.subSequence(
										url.indexOf("android_version_iCard"),url.length()));
			} else {
				cacheDir = makeDirs(
						context,dir
								+ "/"
								+ url.subSequence(
										url.indexOf("android_version_iCard"),url.length()));
				file = new File(cacheDir
						+ "/"
						+ url.subSequence(url.indexOf("android_version_iCard"),url.length()));
			}
		}

		downFile(url);

	}
	
	/**
	 * 
	 *@Description 下载Xml文件
	 * @param xml  下载地址
	 */
	public void downFile(final String xml) {
		new Thread() {
			public void run() {
				super.run();
				URL url = null;
				HttpURLConnection urlConn = null;
				InputStream inputStream = null;

				if (!file.getParentFile().exists()) {
					file.getParentFile().mkdirs();
				}
				FileOutputStream fos = null;
				try {
					url = new URL(xml);
					urlConn = (HttpURLConnection) url.openConnection();
					urlConn.setConnectTimeout(15000);
					inputStream = urlConn.getInputStream();
					if (!file.exists()
							|| (urlConn.HTTP_OK == urlConn.getResponseCode())) {
						fos = new FileOutputStream(file);
						final byte[] bytes = new byte[1024];
						int read = 0;
						while ((read = inputStream.read(bytes)) >= 0) {
							fos.write(bytes,read);
						}
					}
					setConfig(file);

			
				} catch (Exception e) {
					e.printStackTrace();
					try {
						setConfig(file);
					} catch (Exception e1) {
	
						e1.printStackTrace();
					}
					// if (null != file && file.exists()) {
					// file.delete(); // 删除
					// }
				} finally {
					if (inputStream != null) {
						try {
							inputStream.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
					if (urlConn != null) {
						urlConn.disconnect();
					}

					if (null != fos) {
						try {
							fos.close();
						} catch (IOException e) {

							e.printStackTrace();
						}
					}
				}
			}

		}.start();
	}
	
	/**
	 * 
	 *@Description 创建文件夹
	 * @param context
	 * @param path
	 * @return
	 */
	private static File makeDirs(Context context,String path) {
		File cacheDir = null;
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			cacheDir = new File(
					android.os.Environment.getExternalStorageDirectory(),path);
		} else {
			cacheDir = context.getCacheDir();
		}
		if (!cacheDir.exists())
			cacheDir.mkdirs();
		return cacheDir;
	}

	/**
	 * 
	 * @Description 解析XML
	 * @param 文件路径
	 * @throws Exception
	 */
	private void setConfig(File xml) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		InputStream is = new FileInputStream(xml);
		parser.setInput(is,"UTF-8");
		int event = parser.getEventType();
		while (event != parser.END_DOCUMENT) {
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				break;

			case XmlPullParser.START_TAG:
				if (parser.getName().equals("version")) {
					text1 = parser.nextText();
					handler.sendEmptyMessage(0);
				} else if (parser.getName().equals("client_num")) {
					text2 = parser.nextText();
					handler.sendEmptyMessage(0);
				} else if (parser.getName().equals("upgradeURL")) {
					text3 = parser.nextText();
					handler.sendEmptyMessage(0);
				}
				break;

			case XmlPullParser.END_DOCUMENT:
				break;
			}
			event = parser.next();
		}

	}
	
	/**
	 * 在UI上显示
	 */
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				if (!text1.equals("")) {
					show1.setText(text1);
				}
				if (!text2.equals("")) {
					show2.setText(text2);
				}
				if (!text3.equals("")) {
					show3.setText(text3);
				}

			}
			super.handleMessage(msg);
		}

	};
原文链接:https://www.f2er.com/xml/299693.html

猜你在找的XML相关文章