1.写
2.读
package com.example.filedemo;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=new File(Environment.getExternalStorageDirectory()+File.separator+"files/addresslist.xml");
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
try {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.newDocument();
Element addresslist=doc.createElement("addresslist");
Element linkman=doc.createElement("linkman");
Element name=doc.createElement("name");
Element email=doc.createElement("email");
name.appendChild(doc.createTextNode("DanielHan"));
email.appendChild(doc.createTextNode("123131@qq.com"));
linkman.appendChild(name);
linkman.appendChild(email);
addresslist.appendChild(linkman);
doc.appendChild(addresslist);
TransformerFactory formerFac=TransformerFactory.newInstance();
Transformer former=formerFac.newTransformer();
DOMSource source= new DOMSource(doc);
StreamResult result=new StreamResult(file);
former.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}else{
Toast.makeText(this, "没有sdcard", Toast.LENGTH_LONG).show();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/32f58ec2427228feffa40e2967701e94.png)
2.读
package com.example.filedemo;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView name=null;
private TextView email=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name=(TextView)findViewById(R.id.name);
email=(TextView)findViewById(R.id.email);
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file=new File(Environment.getExternalStorageDirectory()+File.separator+"files/addresslist.xml");
if(!file.exists()){//文件不存在
return;
}
try {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.parse(file);
NodeList nodes=doc.getElementsByTagName("linkman");
Element linkname=(Element)nodes.item(0);
name.setText(linkname.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
email.setText(linkname.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
} catch (Exception e) {
e.printStackTrace();
}
}else{
Toast.makeText(this, "没有sdcard", Toast.LENGTH_LONG).show();
}
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/373557ba2db4a6857218f819bf7cf81a.png)