【Android--工具】 SAX解析XML

SAX解析XML是通过逐行扫描文件,他与DOM相比的优势是耗内存小,解析速度快,劣势是流式扫描,不会记录上下节点的关系,遇到符合的就扫描,其节点关系不得而知。


ContentHandler解析器使用方法,常用方法为startDocument、endDocument、startElement、endElement、characters

 1.startElement方法说明

[java]  view plain copy
  1. void startElement(String uri,  
  2.                   String localName,  
  3.                   String qName,  
  4.                   Attributes atts)  
  5.                   throws SAXException  
  6. 方法说明:  
  7. 解析器在 XML 文档中的每个元素的开始调用此方法;对于每个 startElement 事件都将有相应的 endElement 事件(即使该元素为空时)。所有元素的内容都将在相应的 endElement 事件之前顺序地报告。  
  8.   
  9. 参数说明:  
  10. uri - 名称空间 URI,如果元素没有名称空间 URI,或者未执行名称空间处理,则为空字符串  
  11. localName - 本地名称(不带前缀),如果未执行名称空间处理,则为空字符串  
  12. qName - 限定名(带有前缀),如果限定名不可用,则为空字符串  
  13. atts - 连接到元素上的属性。如果没有属性,则它将是空 Attributes 对象。在 startElement 返回后,此对象的值是未定义的  
 2.endElement方法说明
[java]  view plain copy
  1. void endElement(String uri,  
  2.                 String localName,  
  3.                 String qName)  
  4.                 throws SAXException接收元素结束的通知。   
  5. SAX 解析器会在 XML 文档中每个元素的末尾调用此方法;对于每个 endElement 事件都将有相应的 startElement 事件(即使该元素为空时)。  
  6.   
  7. 参数:  
  8. uri - 名称空间 URI,如果元素没有名称空间 URI,或者未执行名称空间处理,则为空字符串  
  9. localName - 本地名称(不带前缀),如果未执行名称空间处理,则为空字符串  
  10. qName - 限定的 XML 名称(带前缀),如果限定名不可用,则为空字符串  

3.characters方法
[java]  view plain copy
  1. void characters(char[] ch,  
  2.                 int start,  
  3.                 int length)  
  4.                 throws SAXException  
  5. 接收字符数据的通知,可以通过new String(ch,start,length)构造器,创建解析出来的字符串文本.  
  6. 参数:  
  7. ch - 来自 XML 文档的字符  
  8. start - 数组中的开始位置  
  9. length - 从数组中读取的字符的个数   

通过SAXParserFactory、SAXParser、XMLReader完成

XML文件:city.xml

<China>
    <province id="34" name="台湾">
        <city id="3401" name="台北">
            <county id="340101" name="台北" weatherCode="101340101"/>
            <county id="340102" name="桃园" weatherCode="101340102"/>
            <county id="340103" name="新竹" weatherCode="101340103"/>
            <county id="340104" name="宜兰" weatherCode="101340104"/>
        </city>
        <city id="3402" name="高雄">
            <county id="340201" name="高雄" weatherCode="101340201"/>
            <county id="340202" name="嘉义" weatherCode="101340202"/>
            <county id="340203" name="台南" weatherCode="101340203"/>
            <county id="340204" name="台东" weatherCode="101340204"/>
            <county id="340205" name="屏东" weatherCode="101340205"/>
        </city>
        <city id="3403" name="台中">
            <county id="340301" name="台中" weatherCode="101340401"/>
            <county id="340302" name="苗栗" weatherCode="101340402"/>
            <county id="340303" name="彰化" weatherCode="101340403"/>
            <county id="340304" name="南投" weatherCode="101340404"/>
            <county id="340305" name="花莲" weatherCode="101340405"/>
            <county id="340306" name="云林" weatherCode="101340406"/>
        </city>
    </province>
</China>

获得InputStream 

InputStream inputStream  = getAssets().open("city.xml");
List<ProvicneModel> provicneModelList = CityParseHandler.getProvince(inputStream);

CityParseHandler解析类

public class CityParseHandler extends BaseParseHandler {

    public static List<ProvicneModel> provicneModelList;
    public  List<AreaModel> areaModelList;
    public  WeatherModel weatherModel;
    private ProvicneModel provicneModel;
    private AreaModel areaModel;
    private CityMode cityMode;

    public  String name;

    public static List<ProvicneModel> getProvince(InputStream inputStream) throws Exception{
        //得到SAX解析工厂
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //创建解析器
        SAXParser saxParser = factory.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        //得到输入流
        InputSource inputSource = new InputSource(inputStream);
        CityParseHandler cityParseHandler = new CityParseHandler();
        xmlReader.setContentHandler(new CityParseHandler());
        xmlReader.parse(inputSource);
        return  CityParseHandler.provicneModelList;
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();    //To change body of overridden methods use File | Settings | File Templates.
        provicneModelList = new ArrayList<ProvicneModel>();
        Log.i(TAG, "startDocument") ;
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();    //To change body of overridden methods use File | Settings | File Templates.
        Log.i(TAG, "endDocument") ;
    }

    @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);    //To change body of overridden methods use File | Settings | File Templates.
        //省
        if(localName.equals("province")){
            System.out.println("localName" + localName);
            provicneModel = new ProvicneModel();
            provicneModel.setCityName(attributes.getValue("name"));
            provicneModel.setCityId(attributes.getValue("id"));
        }
        if(localName.equals("city")){
            cityMode = new CityMode();
            cityMode.setCityName(attributes.getValue("name"));
            cityMode.setCityId(attributes.getValue("id"));
        }
        if(localName.equals("county")){
            areaModel = new AreaModel();
            areaModel.setCityName("name");
            areaModel.setCityId("id");
            areaModel.setWeatherCode("weatherCode");
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);    //To change body of overridden methods use File | Settings | File Templates.
        if(localName.equals("province")){
            provicneModelList.add(provicneModel);
            provicneModel = null;
        }
        if(localName.equals("city")){
            provicneModel.getCityModeList().add(cityMode);
            cityMode= null;
        }
        if(localName.equals("county")){
            cityMode.getAreaModelList().add(areaModel);
            areaModel = null;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);    //To change body of overridden methods use File | Settings | File Templates.
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值