准备一:配置权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
准备二:判断SD卡
if(Environment.getExternalStorageState() .equals(Environment.MEDIA_MOUNTED)) { File file = new File(Environment.getExternalStorageDirectory() + File.separator + DIR + File.separator + FILENAME); }
<?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> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20px" android:text="姓名:" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="李兴华" /> </TableRow> <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20px" android:text="邮箱:" /> <EditText android:id="@+id/email" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="mldnqa@163.com" /> </TableRow> <TableRow> <Button android:id="@+id/but" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="保存" /> </TableRow> </TableLayout>
package org.lxh.demo; 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{ @Override public void onClick(View v) { if (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 不存在不操作 return; // 返回到程序的被调用处 } File file = new File(Environment.getExternalStorageDirectory() + File.separator + "mldndata" + File.separator + "member.xml"); // 要输出文件的路径 // 父路径不存在 if (!file.getParentFile().exists()) { // 创建父文件夹 file.getParentFile().mkdirs() ; } // 1、建立DocumentBuilderFactory,以用于取得DocumentBuilder DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() ; // 2、通过DocumentBuilderFactory取得DocumentBuilder DocumentBuilder builder = null ; try { builder = factory.newDocumentBuilder() ; } catch (ParserConfigurationException e) { e.printStackTrace(); } // 3、定义Document接口对象,通过DocumentBuilder类进行DOM树的转换操作 Document doc = null ; // 创建一个新的文档 doc = builder.newDocument() ; // 4、建立各个操作节点 Element addresslist = doc.createElement("addresslist") ; Element linkman = doc.createElement("linkman") ; Element name = doc.createElement("name") ; Element email = doc.createElement("email") ; // 5、设置节点的文本内容,即为每一个节点添加文本点 name.appendChild(doc.createTextNode(MyDOMDemo.this .name.getText().toString())); email.appendChild(doc.createTextNode(MyDOMDemo.this .email.getText().toString())); // 6、设置节点关系 linkman.appendChild(name) ; linkman.appendChild(email) ; addresslist.appendChild(linkman) ; doc.appendChild(addresslist) ; // 7、输出文档到文件中 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(); } } } }
以下是通过DOM读取XML
package org.lxh.demo; 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; public class MyDOMDemo extends Activity { private TextView name = null; private TextView email = null; private Button but = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); this.name = (TextView) super.findViewById(R.id.name); this.email = (TextView) super.findViewById(R.id.email); this.but = (Button) super.findViewById(R.id.but); this.but.setOnClickListener(new OnClickListenerImpl()); } private class OnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { if (!Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { // 不存在不操作 return; // 返回到程序的被调用处 } File file = new File(Environment.getExternalStorageDirectory() + File.separator + "mldndata" + File.separator + "member.xml"); // 要输出文件的路径 if (!file.exists()) { // 文件不存在 return; } // 1、建立DocumentBuilderFactory,以用于取得DocumentBuilder DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); // 2、通过DocumentBuilderFactory取得DocumentBuilder DocumentBuilder builder = null; try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { e.printStackTrace(); } // 3、定义Document接口对象,通过DocumentBuilder类进行DOM树的转换操作 Document doc = null; try { // 通过文件转化文档 doc = builder.parse(file); } catch (SAXException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } // 4、查找linkman节点 NodeList nl = doc.getElementsByTagName("linkman"); // 5、输出NodeList中第一个子节点中文本节点的内容 for (int x = 0; x < nl.getLength(); x++) { // 取得元素 Element e = (Element) nl.item(x); // 设置文本 MyDOMDemo.this.name.setText(e.getElementsByTagName ("name").item(0).getFirstChild().getNodeValue()); MyDOMDemo.this.email.setText(e.getElementsByTagName ("email").item(0).getFirstChild().getNodeValue()); } } } }
本程序采用DOM解析方式从member.xml文件中将姓名和邮箱的信息读取出来,并且将内容设置到文本显示组件上显示。