本文实例讲述了android中dom解析xml文件的方法。分享给大家供大家参考,具体如下:
一、在assets文件中写xml文件
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version= "1.0" encoding= "utf-8" ?>
<persons>
<person id= "23" >
<name>李明</name>
<age> 30 </age>
</person>
<person id= "20" >
<name>李向梅</name>
<age> 25 </age>
</person>
</persons>
|
二、在service中写一个dom解析的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package com.example.service;
import java.io.inputstream;
import java.util.arraylist;
import java.util.list;
import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import org.w3c.dom.document;
import org.w3c.dom.element;
import org.w3c.dom.node;
import org.w3c.dom.nodelist;
import com.example.domain.person;
public class xmldomservice {
public list<person> parsexml(inputstream is) {
list<person> list = new arraylist<person>();
// 创建dom工厂对象
documentbuilderfactory factory = documentbuilderfactory.newinstance();
try {
// documentbuilder对象
documentbuilder builder = factory.newdocumentbuilder();
// 获取文档对象
document document = builder.parse(is);
// 获取文档对象的root
element root = document.getdocumentelement();
// 获取persons根节点中所有的person节点对象
nodelist personnodes = root.getelementsbytagname( "person" );
// 遍历所有的person节点
for ( int i = 0 ; i < personnodes.getlength(); i++) {
person person = new person();
// 根据item(index)获取该索引对应的节点对象
element personnode = (element) personnodes.item(i); // 具体的person节点
// 设置id属性值
person.setid(integer.parseint(personnode.getattribute( "id" )));
// 获取该节点下面的所有字节点
nodelist personchildnodes = personnode.getchildnodes();
// 遍历person的字节点
for ( int index = 0 ; index < personchildnodes.getlength(); index++) {
// 获取子节点
node node = personchildnodes.item(index);
// 判断node节点是否是元素节点
if (node.getnodetype() == node.element_node) {
//把节点转换成元素节点
element element = (element) node;
//判断元素节点是否是name元素节点
if ( "name" .equals(element.getnodename())) {
person.setname(element.getfirstchild()
.getnodevalue());
} else if ( "age" .equals(element.getnodename())) { //判断是否是age节点
person.setage( new short (element.getfirstchild().getnodevalue()));
}
}
}
// 把person对象加入到集合中
list.add(person);
}
//关闭输入流
is.close();
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return list;
}
}
|
三、在activity中显示操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package com.example.lession03_xml;
import java.io.inputstream;
import java.util.list;
import javax.xml.parsers.saxparser;
import javax.xml.parsers.saxparserfactory;
import com.example.domain.person;
import com.example.service.xmlcontenthandler;
import com.example.service.xmldomservice;
import android.os.bundle;
import android.app.activity;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.inputmethod.inputbinding;
import android.widget.button;
import android.widget.toast;
public class xmlactivityextends activity {
//声明组件
public button btn_sax,btn_dom,btn_pull;
public xmldomservice xmldomservice;
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
//设置显示的视图
setcontentview(r.layout.activity_xml);
xmldomservice= new xmldomservice();
//根据id获取组件
btn_sax=(button) findviewbyid(r.id.btn_sax);
btn_dom=(button) findviewbyid(r.id.btn_dom);
btn_pull=(button) findviewbyid(r.id.btn_pull);
//为按钮注册事件
btn_sax.setonclicklistener( new myonclicklistener());
btn_dom.setonclicklistener( new myonclicklistener());
btn_pull.setonclicklistener( new myonclicklistener());
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.xml, menu);
return true ;
}
//匿名类
class myonclicklistener implements onclicklistener{
@override
public void onclick(view v) {
int id=v.getid();
switch (id) {
case r.id.btn_sax:
toast.maketext(xmlactivity. this , "采用sax解析" , toast.length_long).show();
try {
//sax解析的工厂对象
saxparserfactory factory=saxparserfactory.newinstance();
//得到sax的解析器
saxparser saxparser=factory.newsaxparser();
//创建handler对象
xmlcontenthandler handlerservice= new xmlcontenthandler();
inputstream is=getassets().open( "csdn.xml" );
//直接解析
saxparser.parse(is, handlerservice);
//通过handlerservice对象获取
toast.maketext(xmlactivity. this , "----" +handlerservice, toast.length_long).show();
} catch (exception e){
e.printstacktrace();
}
break ;
case r.id.btn_dom:
inputstream is= null ;
try {
//获取读取文件的输入流对象
is=getassets().open( "csdn.xml" );
//采用dom解析
list<person> persons=xmldomservice.parsexml(is);
//简单测试
//toast.maketext(xmlactivity.this, ""+persons.get(0).getname(), toast.length_long).show();
toast.maketext(xmlactivity. this , "采用dom解析" +persons.get( 0 ).getname(), toast.length_long).show();
} catch (exception e){
e.printstacktrace();
}
break ;
case r.id.btn_pull:
toast.maketext(xmlactivity. this , "采用pull解析" , toast.length_long).show();
break ;
default :
break ;
}
}
}
}
|
希望本文所述对大家android程序设计有所帮助。