您的位置:首页 > 移动开发 > Android开发

Android 解析xml 之DOM

2011-09-13 15:42 381 查看
DOM解析器是通过将XML文档解析成树状模型并将其放入内存来完成解析工作的,而后对文档的操作都是在这个树状模型上完成的。这个在内存中的文档树将是文档实际大小的几倍。这样做的好处是结构清除、操作方便,而带来的麻烦就是极其耗费系统资源。而SAX正好克服了DOM的缺点,分析能够立即开始,而不是等待所有的数据被处理。而且,由于应用程序只是在读取数据时检查数据,因此不需要将数据存储在内存中,这对于大型文档来说是个巨大的优点。事实上,应用程序甚至不必解析整个文档;它可以在某个条件得到满足时停止解析。
文件配置和pull,sax解析一样


package rw.dom;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class XML_DomActivity extends Activity {
/** Called when the activity is first created. */
private Button button;
private ListView listView;
private Student student;
private List<Student> lisStudents=new ArrayList<Student>();
private List<String> list=new ArrayList<String>();
private String Result="";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new ButtonListener());
listView=(ListView)findViewById(R.id.listView1);
}
class ButtonListener implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AssetManager assetManager=getAssets();

DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try {
InputStream inputStream=assetManager.open("student.xml");
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(inputStream);
Element element= document.getDocumentElement();//获取文档的根元素
NodeList nodeList=element.getElementsByTagName("student");  //获取rootElement的所有子节点(不包括属性节点),返回一个NodeList对象
for (int i = 0; i < nodeList.getLength(); i++) {
student=new Student();
Element element2=(Element)nodeList.item(i);
student.setId(Integer.parseInt(element2.getAttribute("id")));
Log.i("------->", element2.getAttribute("id"));
NodeList nodeList2=element2.getChildNodes();
for (int j = 0; j <nodeList2.getLength(); j++) {
Log.i("---------->", String.valueOf(j));
Node node=nodeList2.item(j);
if (node.getNodeType()==Node.ELEMENT_NODE) {
Element element3 =(Element)node;
String elString=element3.getNodeName();
String string=element3.getFirstChild().getNodeValue();
if (elString.equals("name")) {
student.setName(string);
}else if (elString.equals("speciality")) {
student.setSpeciality(string);
}else if (elString.equals("qq")) {
student.setQQ(Integer.parseInt(string));
}
}
}
lisStudents.add(student);
}

} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

for (Student stu : lisStudents) {

list.add(stu.toString());
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}

}
public class Student {

long Id;
String Name;
String Speciality;
long QQ;

public Student(long id, String name, String speciality, long qQ) {
super();
Id = id;
Name = name;
Speciality = speciality;
QQ = qQ;
}

public Student() {
super();
}

public long getId() {
return Id;
}

public String getName() {
return Name;
}

public long getQQ() {
return QQ;
}

public String getSpeciality() {
return Speciality;
}

public void setId(long id) {
Id = id;
}

public void setName(String name) {
Name = name;
}

public void setQQ(long qQ) {
QQ = qQ;
}

public void setSpeciality(String speciality) {
Speciality = speciality;
}
public String toString() {
return (this.getId()+" "+this.getName()+" "+this.getSpeciality()+" "+this.getQQ())+"\n";
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: