package com.m520it.saveandparseXML;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.m520it.temp1.R;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click2SaveXML(View v) {
String passport = "2827887497";
String password = "zhuyanlin";
try {
XMLUtil.saveXMLByStringBuilder(this,passport,password);
} catch (Exception e) {
e.printStackTrace();
}
try {
XMLUtil.saveXMLByXMLSerializer(this,passport,password);
} catch (Exception e) {
e.printStackTrace();
}
}
public void click2ParseXML(View v) {
try {
TextView displayTv = (TextView) findViewById(R.id.display_tv);
String result = XMLUtil.parseXMLByPullParser(this);
displayTv.setText(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
XMLUtil.java
package com.m520it.saveandparseXML;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.OutputStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.content.Context;
import android.util.Xml;
public class XMLUtil {
/**
* 以此方法保存的xml文件效果:
* <map>
* <string name="usename">zhangsan</string>
* <string name="psk">123456</string>
* </map>
* 一定有异常用throw,可能存在异常用throws
*/
public static void saveXMLByStringBuilder(MainActivity mainActivity,
String passport, String password) throws Exception {
//1、建立一个字符串,相比于String,StringBuilder是可变的
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<map>");
stringBuilder.append("<string name="usename">" passport "</string>");
stringBuilder.append("<string name="psk">" password "</string>");
stringBuilder.append("</map>");
String result = stringBuilder.toString();
//2、创建文件的句柄,getFileDir()是在Content类下的,而MainActivity是继承自Content
File f = new File(mainActivity.getFilesDir(),"qqinfo.xml");
//3、使用字符流,并且要进行可能存在异常的检查,我们可以将方法的异常抛出
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
//4、写入刚刚建立的字符串
bw.write(result);
//5、初次之外,还需进行刷新,当然关闭文件也是可以的,里面已经进行了刷新
//bw.flush();
bw.close();
}
/**
* 以此方法保存的xml文件效果:
* <map>
* <username>zhangsan</username>
* <psk>123456</psk>
* </map>
*/
public static void saveXMLByXMLSerializer(MainActivity mainActivity,
String passport, String password) throws Exception {
//1、定义一个xml序列化器
XmlSerializer xmlSerializer = Xml.newSerializer();
//2、需要一个输出流,使用Content中的openFileOutput()比较方便
OutputStream os = mainActivity.openFileOutput("qqinfo1.xml", Context.MODE_PRIVATE);
//3、设置输出路径和格式
xmlSerializer.setOutput(os, "utf-8");
//4、设置xml文件的开始、各个开标签闭标签和标签里面的内容,但是把标签属性 大专栏 Android应用开发之存储和解析xml文件给去掉了
xmlSerializer.startDocument("utf-8", true);
xmlSerializer.startTag(null, "map");
xmlSerializer.startTag(null, "username");
xmlSerializer.text(passport);
xmlSerializer.endTag(null, "username");
xmlSerializer.startTag(null, "psk");
xmlSerializer.text(password);
xmlSerializer.endTag(null, "psk");
xmlSerializer.endTag(null, "map");
xmlSerializer.endDocument();
}
public static String parseXMLByPullParser(MainActivity mainActivity) throws Exception {
String string = "";
//1、创建一个PULL解析器
XmlPullParser xmlPullParser = Xml.newPullParser();
//2、使用流告诉解析器文件的位置和编码
FileInputStream fis = mainActivity.openFileInput("qqinfo1.xml");
xmlPullParser.setInput(fis, "utf-8");
int eventType = xmlPullParser.getEventType();
// 这里要将整型转换成字符串型
// Log.v("520it",eventType "");
//
// eventType = xmlPullParser.next();
// Log.v("520it",eventType "");
// Log.v("520it",xmlPullParser.getName());
//
// eventType = xmlPullParser.next();
// Log.v("520it",eventType "");
// Log.v("520it",xmlPullParser.getName());
//3、查询状态,并取出内容,一直到文档的结束
while(eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_TAG) {
if(xmlPullParser.getName().equals("username")) {
string = "username = " xmlPullParser.nextText() "n";
}else if (xmlPullParser.getName().equals("psk")){
string = "psk = " xmlPullParser.nextText() "n";
}
}
eventType = xmlPullParser.next();
}
return string;
}
}
avtivity_main.xml
按钮不是采用读取id的方法,而是直接使用的onClick属性,仅仅是为了方便,但是在正式的项目中一定要使用读取id设置监听事件的方法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@ id/save_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2SaveXML"
android:text="@string/save_xml" />
<Button
android:id="@ id/parse_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click2ParseXML"
android:text="@string/parse_xml" />
<TextView
android:id="@ id/display_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>