Android基础1: Xml文件解析
2013-01-18 09:44
393 查看
Android 有三种方式进行解析XML: DOM、SAX、PULL
<1> DOM方式:将xml文档全部读入内存,然后使用DOM API访问树形数据,并获取数据,实现简单,但消耗内存
<2> SAX方式:对文件进行顺序扫描,基于事件驱动型解析方式
<3> PULL方式:Pull与SAX类似,但是Pull可以随时跳出解析,而SAX一旦开始就必须要完成将解析工作
使用SAX方式进行解析XML:
<1> DOM方式:将xml文档全部读入内存,然后使用DOM API访问树形数据,并获取数据,实现简单,但消耗内存
<2> SAX方式:对文件进行顺序扫描,基于事件驱动型解析方式
<3> PULL方式:Pull与SAX类似,但是Pull可以随时跳出解析,而SAX一旦开始就必须要完成将解析工作
使用SAX方式进行解析XML:
//根据参数url获得输入源,并使用SAX进行解析XML操作 public void SAXParseFile(URL url){ try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); WeatherHandler handler = new WeatherHandler(); xr.setContentHandler(handler); InputStreamReader isr = new InputStreamReader(url.openStream(),"UTF-8"); InputSource is = new InputSource(isr); xr.parse(is); WeatherSet ws = handler.getWeathers(); } catch(Exception e){ e.printStackTrace(); } } public class WeatherHandler extends DefaultHandler{ private String CURRENT = "current_conditions"; private String FORECAST = "forecast_conditions"; private WeatherSet ws = null; boolean isCurrent = false; boolean isForecast = false; public WeatherHandler(){ } public WeatherSet getWeathers(){ return this.ws; } @Override public void characters(char[] ch, int start, int length) throws SAXException { } @Override public void endDocument() throws SAXException { super.endDocument(); } @Override public void startDocument() throws SAXException { ws = new WeatherSet(); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (localName.equals(CURRENT)) { this.isCurrent = false; } else if (localName.equals(FORECAST)) { this.isForecast = false; } } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(localName); if(localName.equals(CURRENT)){ ws.setCurrent_weather(new CurrentWeather()); isCurrent = true; }else if(localName.equals(FORECAST)){ ws.getForecast_weathers().add(new ForecastWeather()); isForecast = true; }else{ String attr = attributes.getValue("data"); if(localName.equals("icon")){ if(isCurrent){ ws.getCurrent_weather().setIcon(attr); }else if(isForecast){ ws.getLastForecast().setIcon(attr); } } else if(localName.equals("condition")){ if(isCurrent){ ws.getCurrent_weather().setCondition_data(attr); }else if(isForecast){ ws.getLastForecast().setCondition_data(attr); } } else if(localName.equals("temp_c")){ ws.getCurrent_weather().setTemp_c(attr); } else if(localName.equals("temp_f")){ ws.getCurrent_weather().setTemp_f(attr); } else if(localName.equals("humidity")){ ws.getCurrent_weather().setHumidity(attr); } else if(localName.equals("wind_condition")){ ws.getCurrent_weather().setWind_condition(attr); } else if(localName.equals("high")){ ws.getLastForecast().setHigh(attr); } else if(localName.equals("low")){ ws.getLastForecast().setLow(attr); } else if(localName.equals("day_of_week")){ ws.getLastForecast().setWeek_day(attr); } } } }
相关文章推荐
- Android入门学习笔记(一)|基础知识|文件数据存储读取|解析XML
- Android基础 基本的xml文件解析的方式
- Android基础的xml文件创建,解析,以及安卓下的消息机制
- ANDROID基础学习笔记_4.1_采用pull解析xml文件
- android开发第四天xml文件解析
- android pull解析XML文件
- Android 个人学习笔记之---SAX解析XML文件
- Android基础入门教程——7.2.1 Android XML数据解析
- android解析xml文件注意事项
- Android心得3.3--用DOM解析器解析xml文件内容.doc
- AndroidManifest.xml文件解析
- Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(下)
- Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(下)
- Android解析xml文件
- 【转载】Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(下)
- android解析xml文件pull
- Android Animation学习(三) ApiDemos解析:XML动画文件的使用
- AndroidManifest.xml文件解析
- Androidx学习笔记(34)---新闻客户端之获取新闻xml文件并解析
- Android 解析xml文件