<?xml version="1.0" encoding="utf-8"?><channel><item id="0" url="http://www.baidu.com">百度</item><item id="1" url="http://www.qq.com">腾讯</item><item id="2" url="http://www.sina.com.cn">新浪</item><item id="3" url="http://www.taobao.com">淘宝</item></channel>一、使用sax方式解析
public class SAXPraserHelper extends DefaultHandler { final int ITEM = 0x0005; List<channel> list; channel chann; int currentState = 0; public List<channel> getList() {return list; } /** 接口字符块通知*/ @Override public void characters(char[] ch, int start, int length) throws SAXException {// TODO Auto-generated method stub// super.characters(ch, start, length);String theString = String.valueOf(ch, start, length);if (currentState != 0) { chann.setName(theString); currentState = 0;}return; } /** 接收文档结束通知*/ @Override public void endDocument() throws SAXException {// TODO Auto-generated method stubsuper.endDocument(); } /** 接收标签结束通知*/ @Override public void endElement(String uri, String localName, String qName) throws SAXException {// TODO Auto-generated method stubif (localName.equals("item")) list.add(chann); } /** 文档开始通知*/ @Override public void startDocument() throws SAXException {// TODO Auto-generated method stublist = new ArrayList<channel>(); } /** 标签开始通知*/ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {// TODO Auto-generated method stubchann = new channel();if (localName.equals("item")) { for (int i = 0; i < attributes.getLength(); i++) {if (attributes.getLocalName(i).equals("id")) { chann.setId(attributes.getValue(i));} else if (attributes.getLocalName(i).equals("url")) { chann.setUrl(attributes.getValue(i));} } currentState = ITEM; return;}currentState = 0;return; }}private List<channel> getChannelList() throws ParserConfigurationException, SAXException, IOException {//实例化一个SAXParserFactory对象SAXParserFactory factory=SAXParserFactory.newInstance();SAXParser parser;//实例化SAXParser对象,创建XMLReader对象,解析器parser=factory.newSAXParser();XMLReader xmlReader=parser.getXMLReader();//实例化handler,事件处理器SAXPraserHelper helperHandler=new SAXPraserHelper();//解析器注册事件xmlReader.setContentHandler(helperHandler);//读取文件流InputStream stream=getResources().openRawResource(R.raw.channels);InputSource is=new InputSource(stream);//解析文件xmlReader.parse(is);return helperHandler.getList(); }从第二部分代码,可以看出使用SAX解析XML的步骤:private List<Map<String, String>> getData() {List<Map<String, String>> list = new ArrayList<Map<String, String>>();XmlResourceParser xrp = getResources().getXml(R.xml.channels);try { // 直到文档的结尾处 while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {// 如果遇到了开始标签if (xrp.getEventType() == XmlResourceParser.START_TAG) { String tagName = xrp.getName();// 获取标签的名字 if (tagName.equals("item")) {Map<String, String> map = new HashMap<String, String>();String id = xrp.getAttributeValue(null, "id");// 通过属性名来获取属性值map.put("id", id);String url = xrp.getAttributeValue(1);// 通过属性索引来获取属性值map.put("url", url);map.put("name", xrp.nextText());list.add(map); }}xrp.next();// 获取解析下一个事件 }} catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace();} catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();}return list; }三、使用Dom方式解析public static List<channel> getChannelList(InputStream stream) {List<channel> list=new ArrayList<channel>();//得到 DocumentBuilderFactory 对象, 由该对象可以得到 DocumentBuilder 对象DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();try { //得到DocumentBuilder对象 DocumentBuilder builder=factory.newDocumentBuilder(); //得到代表整个xml的Document对象 Document document=builder.parse(stream); //得到 "根节点"Element root=document.getDocumentElement(); //获取根节点的所有items的节点 NodeList items=root.getElementsByTagName("item");//遍历所有节点 for(int i=0;i<items.getLength();i++) {channel chann=new channel();Element item=(Element)items.item(i);chann.setId(item.getAttribute("id"));chann.setUrl(item.getAttribute("url"));chann.setName(item.getFirstChild().getNodeValue());list.add(chann); } } 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();}return list; }总结一下Dom解析的步骤(和sax类似)

源码下载:Android解析XML
原文地址:http://www.cnblogs.com/JerryWang1991
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。