Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
android 解析xml_xml格式文件用什么软件打开,希望能够帮助你!!!。
XmlResourceParser xmlParser = this.getResources().getXml(R.xml.XXX);
b.在res/xml、res/raw目录下:
InputStream inputStream = this.getResources().openRawResource(R.xml.XXX);
InputStream inputStream = getResources().getAssets().open(fileName);
d.在应用指定目录下(SDcard,应用data目录等):
// path路径根据实际项目修改,此次获取SDcard根目录 String path = Environment.getExternalStorageDirectory().toString(); File xmlFlie = new File(path+fileName); InputStream inputStream = new FileInputStream(xmlFlie);
/** * 读取url的xml资源 转成String * @param url * @return 返回 读取url的xml字符串 */ public String getStringByUrl(String url) { String outputString = ""; // DefaultHttpClient DefaultHttpClient httpclient = new DefaultHttpClient(); // HttpGet HttpGet httpget = new HttpGet(url); // ResponseHandler ResponseHandler<String> responseHandler = new BasicResponseHandler(); try { outputString = httpclient.execute(httpget, responseHandler); outputString = new String(outputString.getBytes("ISO-8859-1"), "utf-8"); // 解决中文乱码 Log.i("HttpClientConnector", "连接成功"); } catch (Exception e) { Log.i("HttpClientConnector", "连接失败"); e.printStackTrace(); } httpclient.getConnectionManager().shutdown(); return outputString; }
try { //开始解析事件 int eventType = parser.getEventType(); //处理事件,不碰到文档结束就一直处理 while (eventType != XmlPullParser.END_DOCUMENT) { //因为定义了一堆静态常量,所以这里可以用switch switch (eventType) { case XmlPullParser.START_DOCUMENT: // 不做任何操作或初开始化数据 break; case XmlPullParser.START_TAG: // 解析XML节点数据 // 获取当前标签名字 String tagName = parser.getName(); if(tagName.equals("XXXTAGXXX")){ // 通过getAttributeValue 和 netxText解析节点的属性值和节点值 } break; case XmlPullParser.END_TAG: // 单节点完成,可往集合里边添加新的数据 break; case XmlPullParser.END_DOCUMENT: break; } // 别忘了用next方法处理下一个事件,不然就会死循环 eventType = parser.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }
/** * 同样删除首行,才能解析成功, * @param fileName * @return 返回xml文件的inputStream */ public InputStream getInputStreamFromAssets(String fileName){ try { InputStream inputStream = getResources().getAssets().open(fileName); return inputStream; } catch (IOException e) { e.printStackTrace(); } return null; } /** * 读取XML文件,xml文件放到res/xml文件夹中,若XML为本地文件,则推荐该方法 * * @param fileName * @return : 读取到res/xml文件夹下的xml文件,返回XmlResourceParser对象(XmlPullParser的子类) */ public XmlResourceParser getXMLFromResXml(String fileName){ XmlResourceParser xmlParser = null; try { //*/ // xmlParser = this.getResources().getAssets().openXmlResourceParser("assets/"+fileName); // 失败,找不到文件 xmlParser = this.getResources().getXml(R.xml.provinceandcity); /*/ // xml文件在res目录下 也可以用此方法返回inputStream InputStream inputStream = this.getResources().openRawResource(R.xml.provinceandcity); /*/ return xmlParser; } catch (Exception e) { e.printStackTrace(); } return xmlParser; } /** * 读取url的xml资源 转成String * @param url * @return 返回 读取url的xml字符串 */ public String getStringByUrl(String url) { String outputString = ""; // DefaultHttpClient DefaultHttpClient httpclient = new DefaultHttpClient(); // HttpGet HttpGet httpget = new HttpGet(url); // ResponseHandler ResponseHandler<String> responseHandler = new BasicResponseHandler(); try { outputString = httpclient.execute(httpget, responseHandler); outputString = new String(outputString.getBytes("ISO-8859-1"), "utf-8"); // 解决中文乱码 Log.i("HttpClientConnector", "连接成功"); } catch (Exception e) { Log.i("HttpClientConnector", "连接失败"); e.printStackTrace(); } httpclient.getConnectionManager().shutdown(); return outputString; } /** * 解析SDcard xml文件 * @param fileName * @return 返回xml文件的inputStream */ public InputStream getInputStreamFromSDcard(String fileName){ try { // 路径根据实际项目修改 String path = Environment.getExternalStorageDirectory().toString() + "/test_xml/"; Log.v("", "path : " + path); File xmlFlie = new File(path+fileName); InputStream inputStream = new FileInputStream(xmlFlie); return inputStream; } catch (IOException e) { e.printStackTrace(); } return null; }
b.多线程解析:
/** * 多线程加载网络端的xml,若xml文件过大也需要用该方式加载 */ Handler mHandler = new Handler(); Runnable mRunnable = new Runnable() { public void run() { if(!isFinishParser){ mHandler.postDelayed(mRunnable, 1000); }else{ showView.setText(provinceStr); mHandler.removeCallbacks(mRunnable); } } }; /** * 比较耗时操作新建一个线程,避免UI线程ANR */ public void parserWhitThread(){ new Thread(){ @Override public void run() { provinceandcityStr = getStringByUrl(provinceAndCityUrl); provinceArray = ProvincePullParse.Parse(provinceandcityStr); for(Province pro : provinceArray){ provinceStr += pro.getProvinceId() + " : " +pro.getProvinceName()+"\n"; } isFinishParser = true; } }.start(); }
public static ArrayList<City> ParseXml(XmlPullParser parser){ ArrayList<City> CityArray = new ArrayList<City>(); City CityTemp = null; int provinceId = 0; int cityId; String cityName; try { //开始解析事件 int eventType = parser.getEventType(); //处理事件,不碰到文档结束就一直处理 while (eventType != XmlPullParser.END_DOCUMENT) { //因为定义了一堆静态常量,所以这里可以用switch switch (eventType) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: //给当前标签起个名字 String tagName = parser.getName(); // Log.d("", "====XmlPullParser.START_TAG=== tagName: " + tagName); if(tagName.equals("province")){ provinceId = Integer.parseInt(parser.getAttributeValue(0)); }else if(tagName.equals("item")){ CityTemp = new City(); }else if(tagName.equals("id")){ cityId = Integer.parseInt(parser.nextText()); parser.next(); cityName = parser.nextText(); Log.v("", "id getText: "+cityId); Log.v("", "name getText: "+cityName); Log.e("", "========================="); CityTemp.setProvinceId(provinceId); CityTemp.setCityId(cityId); CityTemp.setCityName(cityName); CityArray.add(CityTemp); } break; case XmlPullParser.END_TAG: break; case XmlPullParser.END_DOCUMENT: break; } //别忘了用next方法处理下一个事件,忘了的结果就成死循环#_# eventType = parser.next(); } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return CityArray; }
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。