使用文件保存数据固然很方便,都是如果现在数据较多的话,管理起来就不方便,所以使用文件保存时,
往往会采用XML文件形式进行数据的保存,那么就要对XML文件进行解析,而DOM解析就是最常用
的一种方式。
在AndroidManifest.xml中配置权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.dom"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".MyDOMDemo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:gravity="center_horizontal"
android:layout_margin="8dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="姓名:" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="李叶文" />
</TableRow>
<TableRow
android:gravity="center_horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="邮箱:" />
<EditText
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="helloliyewen@163.com" />
</TableRow>
<TableRow
android:layout_marginLeft="30dp">
<Button
android:id="@+id/but"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="保存" />
</TableRow>
</TableLayout>
在MyDOMDemo.java程序中
package com.li.dom;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MyDOMDemo extends Activity {
private EditText name = null ;
private EditText email = null ;
private Button but = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.name = (EditText) super.findViewById(R.id.name) ;
this.email = (EditText) super.findViewById(R.id.email) ;
this.but = (Button) super.findViewById(R.id.but) ;
this.but.setOnClickListener(new OnClickListenerImpl()) ;
}
private class OnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) { // 不存在不操作
return; // 返回到程序的被调用处
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "liyewen" + File.separator
+ "test.xml"); // 要输出文件的路径
if (!file.getParentFile().exists()) { // 父路径不存在
file.getParentFile().mkdirs() ; // 创建父文件夹
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() ;
DocumentBuilder builder = null ;
try {
builder = factory.newDocumentBuilder() ;
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null ;
doc = builder.newDocument() ; // 创建一个新的文档
Element addresslist = doc.createElement_x("addresslist") ;
Element linkman = doc.createElement_x("linkman") ;
Element name = doc.createElement_x("name") ;
Element email = doc.createElement_x("email") ;
name.appendChild(doc.createTextNode(MyDOMDemo.this.name.getText()
.toString()));
email.appendChild(doc.createTextNode(MyDOMDemo.this.email.getText()
.toString()));
linkman.appendChild(name) ;
linkman.appendChild(email) ;
addresslist.appendChild(linkman) ;
doc.appendChild(addresslist) ;
TransformerFactory tf = TransformerFactory.newInstance() ;
Transformer t = null ;
try {
t = tf.newTransformer() ;
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
t.setOutputProperty(OutputKeys.ENCODING, "GBK") ;
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file) ;
try {
t.transform(source, result) ;
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
}