用XmlSerializer写xml文件与读xml文件

前端之家收集整理的这篇文章主要介绍了用XmlSerializer写xml文件与读xml文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

写xml文件:在写文件时要判断文件是否存在不存在可以创建

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的文件。用XmlPullParser

XmlPullParser 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文件用XMLResourceParser
Resources 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;
原文链接:https://www.f2er.com/xml/300044.html

猜你在找的XML相关文章