Android基础之XML解析

Android中解析XML一般使用SAX方式

比如Google Weather提供城市名返回天气的XML文件解析

InputStreamReader isr=new InputStreamReader(url.openStream(),"GBK");

InputSource is=new InputSource(isr); 

//三步得到XMLReader对象

SAXParserFactory spf=SAXParserFactory.newInstance();

SAXParser sp=spf.newSAXParser();

XMLReader xr=sp.getXMLReader();

//设置xr对象的ContentHandler             MyContentHandler继承自DefaultHandler,其中规定了解析对应XML文件的算法

xr.setContentHandler(new MyContentHandler());

xr.parse(is);


//规定XML处理规则的Handler

public class MyContentHandler extends DefaultHandler{

               public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {

                                      //localName代表标签的名字,attributes代表标签的属性         PS:字符串的比较用localName.equals("condition")

                                    //进行一些逻辑判断,在对应的标签处把信息存储到要接收的对象中

               }

               public void endElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {

                                    

               }

               public void startDocument() throws SAXException {


              }

               public void endDocument() throws SAXException {

             }

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值