第一步,修改AndroidManifest.xml,添加sdcara写入权限
1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 2 3 <application 4 android:allowBackup="true" 5 android:icon="@drawable/ic_launcher" 6 android:label="@string/app_name" 7 android:theme="@style/AppTheme" > 8 <activity 9 android:name="com.example.androidfilestream04.MainActivity" 10 android:label="@string/app_name" > 11 <intent-filter> 12 <action android:name="android.intent.action.MAIN" /> 13 14 <category android:name="android.intent.category.LAUNCHER" /> 15 </intent-filter> 16 </activity> 17 </application>
第二步,写layout,编写界面
1 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/TableLayout1" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" > 6 7 <TableRow> 8 9 <TextView 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="姓名" /> 13 14 <EditText 15 android:id="@+id/name" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" /> 18 </TableRow> 19 20 <TableRow> 21 22 <TextView 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:text="邮箱" /> 26 27 <EditText 28 android:id="@+id/email" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" /> 31 </TableRow> 32 33 <TableRow> 34 35 <Button 36 android:id="@+id/but" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:text="提交" /> 40 </TableRow> 41 42 </TableLayout>
第三部,编写Activity,进行数据解析
1 package com.example.androidfilestream04; 2 3 import java.io.File; 4 import java.io.StringWriter; 5 6 import javax.xml.parsers.DocumentBuilder; 7 import javax.xml.parsers.DocumentBuilderFactory; 8 import javax.xml.parsers.ParserConfigurationException; 9 import javax.xml.transform.OutputKeys; 10 import javax.xml.transform.Result; 11 import javax.xml.transform.Source; 12 import javax.xml.transform.Transformer; 13 import javax.xml.transform.TransformerConfigurationException; 14 import javax.xml.transform.TransformerException; 15 import javax.xml.transform.TransformerFactory; 16 import javax.xml.transform.dom.DOMSource; 17 import javax.xml.transform.stream.StreamResult; 18 19 import org.w3c.dom.Document; 20 import org.w3c.dom.Element; 21 22 import android.os.Bundle; 23 import android.os.Environment; 24 25 import android.app.Activity; 26 import android.view.Menu; 27 import android.view.View; 28 import android.view.View.OnClickListener; 29 import android.widget.Button; 30 import android.widget.EditText; 31 32 public class MainActivity extends Activity { 33 private EditText name; 34 private EditText email; 35 private Button but = null; 36 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.activity_main); 41 this.name = (EditText) super.findViewById(R.id.name); 42 this.email = (EditText) super.findViewById(R.id.email); 43 this.but = (Button) super.findViewById(R.id.but); 44 this.but.setOnClickListener(new OnClickListenerImpl()); 45 } 46 47 private class OnClickListenerImpl implements OnClickListener { 48 49 @Override 50 public void onClick(View v) { 51 if (!Environment.MEDIA_MOUNTED.equals(Environment 52 .getExternalStorageState())) { 53 return; 54 } 55 // 文件存在地址 56 File file = new File(Environment.getExternalStorageDirectory() 57 + File.separator + "neusoft" + File.separator 58 + "member1.xml"); 59 // 判断路径是否不存在,若不存在则创建 60 if (!file.getParentFile().exists()) { 61 file.getParentFile().mkdirs(); 62 } 63 // 获得dom解析器工厂(工作的作用是用于创建具体的解析器) 64 DocumentBuilderFactory factory = DocumentBuilderFactory 65 .newInstance(); 66 DocumentBuilder builder = null; 67 try { 68 // 获得具体的dom解析器 69 builder = factory.newDocumentBuilder(); 70 } catch (ParserConfigurationException e) { 71 // TODO Auto-generated catch block 72 e.printStackTrace(); 73 } 74 Document doc = null; 75 doc = builder.newDocument();// 创建一个新的文档 76 Element addresslist = doc.createElement("addresslist"); 77 Element linkman = doc.createElement("linkman"); 78 Element name = doc.createElement("name"); 79 Element email = doc.createElement("email"); 80 // 设置标签显示的内容 81 name.appendChild(doc.createTextNode(MainActivity.this.name 82 .getText().toString())); 83 email.appendChild(doc.createTextNode(MainActivity.this.email 84 .getText().toString())); 85 86 // 设置标签之间的关系 87 linkman.appendChild(name); 88 linkman.appendChild(email); 89 addresslist.appendChild(linkman); 90 // 最后放入文档 91 doc.appendChild(addresslist); 92 93 // 取得TransformerFactory实例 94 TransformerFactory tf = TransformerFactory.newInstance(); 95 Transformer t = null; 96 97 try { 98 // 从transFactory获取Transformer实例 99 t = tf.newTransformer(); 100 } catch (TransformerConfigurationException e) { 101 // TODO Auto-generated catch block 102 e.printStackTrace(); 103 } 104 t.setOutputProperty(OutputKeys.ENCODING, "utf-8"); 105 DOMSource source = new DOMSource(doc);// 输出文档 106 StreamResult result = new StreamResult(file);// 指定输出位置 107 try { 108 t.transform(source, result);// 输出 109 } catch (TransformerException e) { 110 // TODO Auto-generated catch block 111 e.printStackTrace(); 112 } 113 114 } 115 116 } 117 118 }
时间: 2024-10-12 13:23:51