接上一章,这一章我们就来学习SAX解析器,其实SAX跟PULL解析器差不多,事实上学会和掌握一种XML解析器对于完成项目来说已经足够了,当然,多学一点也不是什么坏事,你说呢?呵呵,加油哦!我们还是以唐僧师徙的数据为例
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id = "1"> <status>大徙弟</status> <name>孙悟空</name> <tool>金箍棒</tool> <number>杀死了50只妖怪</number> </person> <person id = "2"> <status>二徙弟</status> <name>猪八戒</name> <tool>九齿钉耙 </tool> <number>杀死了40只妖怪</number> </person> <person id = "3"> <status>三徙弟</status> <name>沙和尚</name> <tool>降妖宝杖</tool> <number>杀死了30只妖怪</number> </person> </persons>
xml文件跟上一章的一样,如果不清楚的话可以参考一下上一章
这里我就主要放上SAXHandler的源码了
import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxHandler extends DefaultHandler { private List<Person> persons; private Person person; private String tagName;//作用是记录解析时的上一个节点名称 public List<Person> getPersons(){ return persons; } /** * 节点处理 */ @Override public void characters(char[] ch, int start, int length) throws SAXException { String data = new String(ch, start, length); if("status".equals(tagName)){ person.setStatus(data); } if("name".equals(tagName)){ person.setName(data); } if("tool".equals(tagName)){ person.setTool(data); } if("number".equals(tagName)){ person.setNumber(data); } } /** * 元素结束 */ @Override public void endElement(String uri, String localName, String qName) throws SAXException { if("person".equals(localName)){ persons.add(person); person = null; } tagName = null; } /** * 文档开始 */ @Override public void startDocument() throws SAXException { persons = new ArrayList<Person>(); } /** * 元素开始 */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("person".equals(localName)){ person = new Person(); person.setId(attributes.getValue("id")); } tagName = localName;//将正在解析的节点名称赋给tagName } }
接着就是Activity中的源码:
import java.io.File; import java.io.FileInputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.widget.ListView; import android.widget.SimpleAdapter; public class PullActivity extends Activity { private ListView listView; private SimpleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.xml_handler); listView = (ListView)findViewById(R.id.xml_list); try { //自完义适配方法 getAdapter(); } catch (Exception e) { e.printStackTrace(); } listView.setAdapter(adapter); } //自定义适配方法 private void getAdapter() throws Exception { List<Map<String, String>> lists = new ArrayList<Map<String, String>>(); //这一部分就是刚才我们做测试用的部分 File SD_Files = Environment.getExternalStorageDirectory(); String file_path = SD_Files.getName() + File.separator + "persons.xml"; // FileInputStream input = new FileInputStream(new File(file_path)); // PullHandler pullHandler = new PullHandler(input); // List<Person> persons = pullHandler.getPersons(); FileInputStream fis = new FileInputStream(new File(file_path)); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); SaxHandler saxHandler = new SaxHandler(); parser.parse(fis, saxHandler); List<Person> persons = saxHandler.getPersons(); //将persons中的数据转换到ArrayList<Map<String, String>>中,因为SimpleAdapter要用这个类型的数据进行适配 Map<String, String> map; for(Person p : persons){ map = new HashMap<String, String>(); map.put("id", p.getId()); map.put("status", p.getStatus()); map.put("name", p.getName()); map.put("tool", p.getTool()); map.put("number", p.getNumber()); lists.add(map); } //HashMap<String, String>中的key String[] from = {"id", "status", "name", "tool", "number"}; //list_item.xml中对应的控件ID int[] to = {R.id.item_id, R.id.item_status, R.id.item_name, R.id.item_tool, R.id.item_number}; adapter = new SimpleAdapter(this, lists, R.layout.handler_list_item, from, to); } }
其他的跟PULL解析都一样的,这里只是将PULL解析换成了SAX解析,在源码中就是把中间的三行代码换成了SAX解析的代码,上图:
看,都是一样的,下一章要介绍DOM解析器了,希望大家能好好学习,谢谢