Android-解析XML(Dom,Sax,Pull)

//Dom解析
public void ParseXML(View view) {
            new MyThread().execute();
           //02.解析xml
    }
       //异步任务类
    class MyThread extends AsyncTask{
           @Override
           protected Object doInBackground(Object[] params) {
               try {
                   URL url = new URL("http://www.w3school.com.cn/example/xmle/plant_catalog.xml");
                   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                   //获得请求方式
                   connection.setRequestMethod("GET");
                   //设置请求连接超时的时间
                   connection.setConnectTimeout(5000);
                   //获取结果码
                   int code=connection.getResponseCode();
                   if (code==200){
                       //获取服务器返回来的结果
                       InputStream inputStream=connection.getInputStream();
                       //打印
                      /* BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                       String str=null;
                       while ((str=bufferedReader.readLine())!=null){
                           Log.i("test",str);
                       }*/
                       //使用Dom解析
                       DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
                       DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
                       Document document=documentBuilder.parse(inputStream);
                        //获取跟标签
                       Element element=document.getDocumentElement();
                       Log.i("test"," 跟标签 "+element.getNodeName());
                        //获取跟标签下面的子标签
                       NodeList nodeList=element.getElementsByTagName("PLANT");
                       for (int i = 0; i < nodeList.getLength(); i++) {
                           Element personElement= (Element) nodeList.item(i);
                          //获取跟标签下面的内容
                          Element COMMON= (Element) personElement.getElementsByTagName("COMMON").item(0);
                          String COMMONname=COMMON.getTextContent();
                          Element BOTANICAL= (Element) personElement.getElementsByTagName("BOTANICAL").item(0);
                          String BOTANICALNname=BOTANICAL.getTextContent();
                          Element ZONE= (Element) personElement.getElementsByTagName("ZONE").item(0);
                          String ZONENname=ZONE.getTextContent();
                          Log.i("test",COMMONname+" "+BOTANICALNname+"  "+ZONENname);
                      }
                   }
               } catch (MalformedURLException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               } catch (ParserConfigurationException e) {
                   e.printStackTrace();
               } catch (SAXException e) {
                   e.printStackTrace();
               }
               return null;
               }

           @Override
           protected void onPostExecute(Object o) {
               super.onPostExecute(o);
               //03.展示
           }
       }
//Sax解析
            
                       
SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();
                       SAXParser saxParser=saxParserFactory.newSAXParser();
                       saxParser.parse(inputStream,new DefaultHandler(){
                           @Override
                           public void endDocument() throws SAXException {
                               super.endDocument();}
                           @Override
                           public void startDocument() throws SAXException {
                               super.startDocument();}
                           @Override
                           public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                               super.startElement(uri, localName, qName, attributes);
                               currentTag=localName;
                               Log.i("ccc",localName);}
                           @Override
                           public void endElement(String uri, String localName, String qName) throws SAXException {
                               super.endElement(uri, localName, qName);
                              currentTag=null;
                       }
                             @Override
                               public void characters(char[] ch, int start, int length) throws SAXException {
                                   super.characters(ch, start, length);
                               //正文
                                if("COMMON".equals(currentTag)){
                                    //获得值
                                    String COMMON=new String(ch,start,length);
                                    Log.i("test","    "+COMMON);
                                }else if ("BOTANICAL".equals(currentTag)){
                                    String BOTANICAL=new String(ch,start,length);
                                    Log.i("test","    "+BOTANICAL);
                                }
                              }
                            });}
               } catch (MalformedURLException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               } catch (ParserConfigurationException e) {
                   e.printStackTrace();
               } catch (SAXException e) {
                   e.printStackTrace();
               }
                return null;
               }
           @Override
           protected void onPostExecute(Object o) {
               super.onPostExecute(o);
  
           }



 
  
 
  
//Pull解析
 
  
  //pull解析
                   XmlPullParser xmlpullparser= Xml.newPullParser();
                   xmlpullparser.setInput(inputStream,"utf-8");
                    //获取解析的标签类型
                  int type=xmlpullparser.getEventType();
                   while (type!=XmlPullParser.END_DOCUMENT){
                         switch (type){
                             case XmlPullParser.START_TAG:
                                 //获取开始标签的名字
                                 String starttagname=xmlpullparser.getName();
                                 if ("COMMON".equals(starttagname)){
                                     String common=xmlpullparser.nextText();
                                     Log.i("test",common);}
                                      break;
                             case XmlPullParser.END_TAG:
                                 break;}
                           type=xmlpullparser.next();
                    }
               } catch (MalformedURLException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               } 
catch (XmlPullParserException e) {
                   e.printStackTrace();
               }
           
               return null;
               }
           @Override
           protected void onPostExecute(Object o) {
               super.onPostExecute(o);
               //03.展示
           }

 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(xml,android)