今天学习Android数据存储——文件存储_DOM解析XML文档
位于org.w3c.dom操作XML会比较简单,就是将XML看做是一颗树,DOM就是对这颗树的一个数据结构的描述,但对大型XML文件效果可能会不理想
首先来了解点Java DOM 的 API:
1.解析器工厂类:DocumentBuilderFactory
创建的方法:DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
2.解析器:DocumentBuilder
创建方法:通过解析器工厂类来获得 DocumentBuilder builder = factory.newDocumentBuilder();
3.文档树模型Document
创建方法:
a.通过xml文档 Document doc = builder.parse("member.xml");
b.将需要解析的xml文档转化为输入流 InputStream input = new FileInputStream("member.xml");
Document doc =builder.parse(input );
Document对象代表了一个XML文档的模型树,所有的其他Node都以一定的顺序包含在Document对象之内,排列成一个树状结构,以后对XML文档的所有操作都与解析器无关,直接在这个Document对象上进行操作即可;
包含的方法:
4.节点列表类NodeList
NodeList代表了一个包含一个或者多个Node的列表,根据操作可以将其简化的看做为数组
5.节点类Node
Node对象是DOM中最基本的对象,代表了文档树中的抽象节点。但在实际使用中很少会直接使用Node对象,而是使用Node对象的子对象Element,Attr,Text等
6.元素类Element
是Node类最主要的子对象,在元素中可以包含属性,因而Element中有存取其属性的方法
7.属性类Attr
代表某个元素的属性,虽然Attr继承自Node接口,但因为Attr是包含在Element中的,但并不能将其看做是Element的子对象,因为Attr并不是DOM树的一部分
基本的知识就到此结束,更加具体的大家可以参阅JDK API文档
使用DOM输出xml文件
XML布局:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyDOMDemo" >
<TableRow >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="姓名:"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="李兴华"/>
</TableRow>
<TableRow >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="邮箱:"/>
<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="mldnqa@163.com"/>
</TableRow>
<TableRow >
<Button
android:id="@+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存"/>
</TableRow>
</TableLayout>
Java代码:
public class MyDOMDemo extends Activity {
private TextView name = null;
private TextView email = null;
private Button but = null;
@Override
protected 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) {
/**
* DOM输出xml文件
*/
if(!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)){//SD卡不存在则不操作
return;//返回到程序的被调用处
}
File file = new File(Environment.getExternalStorageDirectory()+File.separator+"mldndata"+File.separator+"member.xml");//要输出的文件路径
if(!file.getParentFile().exists()){//父路径不存在
file.getParentFile().mkdirs();//创建父文件夹
}
//实例化一个DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
//创建一个新的DocumentBuilder
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
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(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
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = null;
try {
//创建一个新的Transformer
t = tf.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
//设置输出属性,编码为GBK
t.setOutputProperty(OutputKeys.ENCODING, "GBK");
DOMSource source = new DOMSource(doc);//XML源文档
StreamResult result = new StreamResult(file);//输出目标
try {
t.transform(source, result);//输出xml文件
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
}
查看产生的XML文件
使用DOM读取xml文件
XML布局:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyDOMDemo" >
<TableRow >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="姓名:"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="邮箱:"/>
<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow >
<Button
android:id="@+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取"/>
</TableRow>
</TableLayout>
Java代码:
public class MyDOMDemo extends Activity {
private TextView name = null;
private TextView email = null;
private Button but = null;
@Override
protected 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) {
/**
* DOM读取xml文件
*/
if(!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)){//SD卡不存在则不操作
return;//返回到程序的被调用处
}
File file = new File(Environment.getExternalStorageDirectory()
+File.separator+"mldndata"+File.separator
+"member.xml");//要输出的文件路径
if(!file.exists()){//文件不存在
return;
}
//实例化一个DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
//创建一个新的DocumentBuilder
builder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(file);//通过xml文件转化为文档
} catch (SAXException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
//从文档中获得结点列表
NodeList n1 = doc.getElementsByTagName("linkman");
for(int x=0;x<n1.getLength();x++){
Element e = (Element)n1.item(x);//取得元素
MyDOMDemo.this.name.setText(e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
MyDOMDemo.this.email.setText(e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
}
}
}
}
读取结果: