xmlfile.mkdir();//创建文件夹
xmlfile.createNewFile();//创建文件
private XmlSerializer serializer;
private void writeXML() { // 取到更改后的值 List<HashMap<String,String>> relist = new ArrayList<HashMap<String,String>>(); //在adapter中取到更改以后的值 relist = adapter.getData(); //测试是否能取到正确的值 for (int i = 0; i < relist.size(); i++) { String reid = relist.get(i).get("id").toString(); System.out.println("---activity reid ---"+reid); String revalue = relist.get(i).get("value").toString(); System.out.println("---activity revalue ---"+revalue); } try { //向data/data/包名/files下的appserver.xml文件写 //OutputStream out = this.openFileOutput("appserver.xml",// Context.MODE_PRIVATE); //OutputStreamWriter outw = new OutputStreamWriter(out); String xmlPath = File.separator + "sdcard" + File.separator+"appserver.xml"; File xmlFile = new File(xmlPath); FileOutputStream fileos = new FileOutputStream(xmlFile); serializer = Xml.newSerializer(); //serializer.setOutput(outw); serializer.setOutput(fileos,"UTF-8");//设置输出的writer对象 serializer.startDocument("UTF-8",true); //写入xml标识语句 serializer.startTag(null,"resources");// 开始根元素标签<resources> for (int i = 0; i < relist.size(); i++) { serializer.startTag(null,"app");//写入xml开始元素标识 serializer.attribute(null,"id",relist.get(i).get("id")); serializer.attribute(null,"value",relist.get(i).get("value"));//写属性 serializer.text("aaa");//写入元素的值 serializer.endTag(null,"app"); } // 结束标签</resources> serializer.endTag(null,"resources"); // 结束文档标记 serializer.endDocument(); // System.out.println("----save file----"); serializer.flush(); // 将serializer中数据写入文件 fileos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
读xml文件:1、读取res下的xml的文件。用XmlPullParserXmlPullParser xrp =null; InputStream slideInputStream = null; AppServerVO appSerer = AppServerVO.getInstance(); String xmlPath = File.separator + "sdcard" + File.separator +"appserver.xml"; File xmlFile = new File(xmlPath); if(xmlFile.exists()){ System.out.println("--文件存在---"); try { slideInputStream = new FileInputStream(xmlPath); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); xrp = factory.newPullParser(); xrp.setInput(slideInputStream,"UTF-8"); while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { if (xrp.getEventType() == XmlResourceParser.START_TAG) { String s = xrp.getName(); if (s.equals("app")) { String id = xrp.getAttributeValue(null,"id"); String value = xrp.getAttributeValue(null,"value"); if ("name".equals(id)) { appSerer.setName(value); } else if ("sdkType".equals(id)) { appSerer.setSdkType(value); } } } xrp.next(); } return appSerer; } catch (Exception e) { e.printStackTrace(); } finally { //关闭输入流 if (slideInputStream != null) { try { slideInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } slideInputStream = null; } } }else{ System.out.println("--文件不存在---"); } return null;2、读取指定目录下的xml文件用XMLResourceParserResources res = getResources(); XmlResourceParser xrp = res.getXml(R.xml.appserver);
while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) { if (xrp.getEventType() == XmlResourceParser.START_TAG) { String s = xrp.getName(); if (s.equals("app")) { String id = xrp.getAttributeValue(null,"id"); String value = xrp.getAttributeValue(null,"value"); if ("name".equals(id)) { appSerer.setName(value); } else if ("sdkType".equals(id)) { appSerer.setSdkType(value); } } } xrp.next(); } return appSerer; } catch (Exception e) { } finally { xrp.close(); } return null;