SAX解析xml

xml

<books xmlns:xx="www.bb.com">
    <xx:book xx:id="1">
        <name ss="lirui">happy day</name> 
        <author>li</author>
    </xx:book>
    <book id="2">
        <name>bad bed</name>
        <author>lirui</author>
    </book>
    <book id="3">
        <name>godlike</name> 
        <author>whattff</author>
    </book>
</books>
InputStream in=getResources().openRawResource(R.raw.book); 

        SAXParserFactory factory =SAXParserFactory.newInstance(); 
        try {
            SAXParser parser=factory.newSAXParser();
            SaxHandler handler=new SaxHandler(); 
            parser.parse(in, handler); 

        } 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();
        } 
private class SaxHandler extends DefaultHandler{
        @Override
        public void startDocument() throws SAXException{
            Log.i(TAG,"startDocument"); 
            super.startDocument(); 
        }
        @Override
        public void startElement(String arg0,String arg1,String arg2,Attributes arg3) throws SAXException{
            Log.i(TAG,"startElement"); 
            Log.i(TAG,"arg0 "+arg0); 
            Log.i(TAG,"arg1 "+arg1); 
            Log.i(TAG,"arg2 "+arg2);  
            Log.i(TAG,"Attributes : "); 
            if (arg3!=null){
                for (int i=0;i<arg3.getLength();i++){
                    Log.i(TAG,arg3.getQName(i)+"  "+arg3.getValue(i)); 
                }
            }
            super.startElement(arg0, arg1, arg2, arg3); 
        }
        @Override
        public void characters(char[] arg0,int arg1,int arg2) throws SAXException{
            String ss=new String(arg0,arg1,arg2); 
            Log.i(TAG,ss); 
            super.characters(arg0, arg1, arg2); 
        }

        @Override
        public void endElement(String arg0,String arg1,String arg2) throws SAXException{ 
            Log.i(TAG,"endElement"); 
            super.endElement(arg0, arg1, arg2); 
        }
        @Override
        public void endDocument() throws SAXException{
            Log.i(TAG,"endDocument"); 
            super.endDocument(); 
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值