Android 读写XML文件(使用pull解析)

pull解析xml文件代码如下:

public void PullParseXML(String filepath) {
		String res = "本结果是通过XmlPullParse解析:" + "/n";
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser xmlPullParser = factory.newPullParser();

			File file = new File(filepath);
			if(!file.exists()){
				CreateXML();
				file = new File(filepath);
			}
			FileInputStream input=null;
			try {
				input = new FileInputStream(file);
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if(input == null)
				return;
			xmlPullParser.setInput(input,"UTF-8");

			int eventType = xmlPullParser.getEventType();
			String str="";

			try {
				while (eventType != XmlPullParser.END_DOCUMENT) {
					String nodeName = xmlPullParser.getName();
					switch (eventType) {
					case XmlPullParser.START_TAG:
						if ("XX1".equals(nodeName)) {
							str = xmlPullParser.nextText();
							res += "XX1 = "+str+"--";
						} else if ("XX2".equals(nodeName)) {
							str = xmlPullParser.nextText();
							res += "XX2 = "+str+"--";
						}else if ("XX3".equals(nodeName)) {
							str = xmlPullParser.nextText();
							res += "XX3 = "+str+"--";
						}else if ("XX4".equals(nodeName)) {
							str=xmlPullParser.nextText();
							res += "XX4 = "+str+"--";
						}else if ("XX5".equals(nodeName)) {
							str = xmlPullParser.nextText();
							res += "XX5 = "+str+"--";
						}
						break;
					default:
						break;
					}
					eventType = xmlPullParser.next();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return;
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return;
		}
		Log.e("PullParseXML:",res);
	}

2.写xml文件如下:

public void WriteXml(String filepath) throws IllegalArgumentException, IllegalStateException, IOException {
		File file = new File(filepath);
		if(!file.exists()){
			CreateXML();
			file = new File(filepath);
		}
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(out == null)
			return;
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(out, "UTF-8");
        serializer.startDocument("UTF-8", true);
        
        serializer.startTag("", "SettingInfo");

        String str = "0";//在此先随便写个数据
		serializer.startTag("", "XX1");
		serializer.text(str);
		serializer.endTag("", "XX1");

		serializer.startTag("", "XX2");
		serializer.text(str);
		serializer.endTag("", "XX2");

		str = "0";
		serializer.startTag("", "XX3");
		serializer.text(str);
		serializer.endTag("", "XX3");

		serializer.startTag("", "XX4");
		serializer.text(str);
		serializer.endTag("", "XX4");

		serializer.startTag("", "XX5");
		serializer.text(str);
		serializer.endTag("", "XX5");

		serializer.endTag("", "SettingInfo");
        serializer.endDocument();
        out.flush();
        out.close();
    }



---应该有获取xml的tag直接进行数据修改,会的童鞋还请在告知下,谢谢!


你可能感兴趣的:(android)