Android-분석 XML(Dom,Sax,Pull)

8237 단어 xmlandroid
//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.  
           }

 
  
 
  
 
  
 
  
 
 

좋은 웹페이지 즐겨찾기