dom操作:
配置权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
判断sdcard是否存在
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// sdcard不存在
return;// 返回程序被调用处
}
1.dom保存数据到xml文件
step1 添加如下在manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activity"
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=".DOMProjectActivity"
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>
step2编写如下 在main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20px" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="小马哥"
android:textSize="20px" />
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="邮箱:"
android:textSize="20px" />
<EditText
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="qq@qq.com"
android:textSize="20px" />
</TableRow>
<TableRow>
<Button
android:id="@+id/but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存"
android:textSize="20px" />
</TableRow>
</TableLayout>
step3.编写activity
package com.activity;
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.TextView;
/**
* dom保存数据到xml文件
* @author Administrator
*
*/
public class DOMProjectActivity extends Activity {
/** Called when the activity is first created. */
private TextView nameView = null;
private TextView emailView = null;
private Button but = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.nameView = (TextView) findViewById(R.id.name);
this.emailView = (TextView) findViewById(R.id.email);
this.but = (Button) findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// sdcard不存在
return;// 返回程序被调用处
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "qqdata" + File.separator + "qq.xml");// 要输出文件的路径
if (!file.getParentFile().exists()) {// 父路径不存在
file.getParentFile().mkdirs();// 创建父文件夹
}
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document document = null;
document = builder.newDocument();// 创建一个新的文档
Element addresslist = document.createElement("addresslist");
Element linkman = document.createElement("linkman");
Element name = document.createElement("name");
Element email = document.createElement("email");
name.appendChild(document
.createTextNode(DOMProjectActivity.this.nameView.getText()
.toString()));
email.appendChild(document
.createTextNode(DOMProjectActivity.this.emailView.getText()
.toString()));
linkman.appendChild(name);
linkman.appendChild(email);
addresslist.appendChild(linkman);
document.appendChild(addresslist);
TransformerFactory tFactory=TransformerFactory.newInstance();
Transformer transformer=null;
try {
transformer=tFactory.newTransformer();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
DOMSource source=new DOMSource(document);
StreamResult result=new StreamResult(file);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2.dom读取数据从xml
step1 编写main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="20px" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px" />
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="邮箱:"
android:textSize="20px" />
<EditText
android:id="@+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px" />
</TableRow>
<TableRow>
<Button
android:id="@+id/but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存"
android:textSize="20px" />
</TableRow>
</TableLayout>
step2 编写activity
package com.activity;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
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.TextView;
/**
* dom读取数据从xml文件
*
* @author Administrator
*
*/
public class DOMProjectActivity extends Activity {
/** Called when the activity is first created. */
private TextView nameView = null;
private TextView emailView = null;
private Button but = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.nameView = (TextView) findViewById(R.id.name);
this.emailView = (TextView) findViewById(R.id.email);
this.but = (Button) findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {// sdcard不存在
return;// 返回程序被调用处
}
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "mldndata" + File.separator + "mldn.xml");// 要输出文件的路径
if (!file.exists()) {// 父路径不存在
return;
}
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Document document = null;
try {
document = builder.parse(file);// 通过文件转换文档
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NodeList nList = document.getElementsByTagName("linkman");
for (int x = 0; x < nList.getLength(); x++) {
Element element = (Element) nList.item(x);// 取得元素
DOMProjectActivity.this.nameView.setText(element
.getElementsByTagName("name").item(0).getFirstChild()
.getNodeValue());
DOMProjectActivity.this.emailView.setText(element
.getElementsByTagName("email").item(0).getFirstChild()
.getNodeValue());
}
}
}
}