SAX解析xml

来源:互联网 发布:ubuntu for arm 编辑:程序博客网 时间:2024/05/29 18:33

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();         }    }
0 0
原创粉丝点击