一、在assets文件中写xml文件
<?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解析的操作
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中显示操作
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 XmlActivity extends 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;
}
}
}
}