Android基础1: Xml文件解析

来源:互联网 发布:数据分析助力销售业绩 编辑:程序博客网 时间:2024/06/10 11:50

Android 有三种方式进行解析XML: DOM、SAX、PULL

<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);           }       }    } }

原创粉丝点击