使用PullParser解析属性中的值的方法

时间:2011-08-10 07:05:18

标签: android

我想解析谷歌天气Api的回复..      回应如下:

  <current_conditions>
  <condition data="Haze"/>
  <temp_f data="82"/>
  <temp_c data="28"/>
  <humidity data="Humidity: 94%"/>
  <icon data="/ig/images/weather/haze.gif"/>
  <wind_condition data="Wind: SW at 12 mph"/>
  </current_conditions>      

     now i want only temp_c data to use in my app. 
     so how can i get the value of temp using xml pull parser..

     right now i m doing like that 

   StringBufferInputStream buffer = new StringBufferInputStream(response);
        xpp = XmlPullParserFactory.newInstance().newPullParser();
        xpp.setInput(buffer, null);
        int eventType = xpp.getEventType();
        while(eventType != XmlPullParser.END_DOCUMENT)
        {


            if(eventType == XmlPullParser.START_TAG){

                String elementName = xpp.getName();
                if(elementName.equals("current_conditions")){
                    local_pickup = xpp.nextText();
                    Log.d("database", local_pickup);
                }
                       }
                   eventType = xpp.next();
        }
        buffer.close();

请尽早帮助我们

1 个答案:

答案 0 :(得分:1)

以下是使用Android XML Pull Parser解析单个值的一个很好的示例。

除上述链接外

package com.news.search;

import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

import org.xmlpull.v1.XmlPullParser; import android.util.Xml;

import com.yahoo.search.ImageThumbnail; import com.yahoo.search.NewsSearchResult; import com.yahoo.search.xmlparser.XmlParserImageThumbnail; import com.yahoo.xml.XmlParser;


public class NewsPullParser {

private InputStream input;    
public final static String TITLE = "Title";  
public final static String SUMMARY = "Summary";  
public final static String URL = "Url";  
public final static String CLICK_URL = "ClickUrl";  
public final static String SOURCE = "NewsSource";  
public final static String SOURCE_URL = "NewsSourceUrl";  
public final static String LANGUAGE = "Language";  
public final static String PUBLISH_DATE = "PublishDate";  
public final static String MOD_DATE = "ModificationDate";  
public static ArrayList newsTags;  
static{    newsTags = new ArrayList();
           newsTags.add(TITLE);
           newsTags.add(SUMMARY);
           newsTags.add(URL);
           newsTags.add(CLICK_URL);    
           newsTags.add(SOURCE);   
           newsTags.add(SOURCE_URL);    
           newsTags.add(LANGUAGE);    
           newsTags.add(PUBLISH_DATE);    
           newsTags.add(MOD_DATE);  
}



public NewsPullParser(InputStream stream){   this.input = stream; }  
public NewsSearchResult[] parse(List TagFilter) throws Exception {
     ArrayList content = null;   
     XmlPullParser parser = Xml.newPullParser();
       try{

    parser.setInput(this.input,"UTF-8");
    HashMap map = null;
    int event = parser.getEventType();

    while (event != XmlPullParser.END_DOCUMENT){
     String name = null;

     switch (event){

     case XmlPullParser.START_DOCUMENT: content = new ArrayList();
                                        break;
     case XmlPullParser.START_TAG:
      name = parser.getName();
      if (name.equalsIgnoreCase("Result")){
        map = new HashMap();
      }else if(map != null && newsTags.contains(name) && TagFilter.contains(name)){

        map.put(name,parser.nextText());

      }
      break;

     case XmlPullParser.END_TAG:
      name = parser.getName();
      if (name.equalsIgnoreCase("RESULT") && map != null)
       content.add(new PullParserNewsSearchResult(map));

      break;
     }

     event = parser.next();
    }

          }catch(Exception exp){    throw exp;   }
      return content.toArray(new NewsSearchResult[content.size()]);  }

    private class PullParserNewsSearchResult implements NewsSearchResult {
         private Map result;


         public PullParserNewsSearchResult(Map result) {
             this.result = result;
         }

         public String getTitle() {
             return (String)result.get(TITLE);
         }

         public String getSummary() {
             return (String)result.get(SUMMARY);
         }

         public String getUrl() {
             return (String)result.get(URL);
         }

         public String getClickUrl() {
             return (String)result.get(CLICK_URL);
         }

         public String getNewsSource() {
             return (String)result.get(SOURCE);
         }

         public String getNewsSourceUrl() {
             return (String)result.get(SOURCE_URL);
         }

         public String getLanguage() {
             return (String)result.get(LANGUAGE);
         }

         public String getPublishDate() {
             return (String)result.get(PUBLISH_DATE);
         }

         public String getModificationDate() {
             return (String)result.get(MOD_DATE);
         }

         public ImageThumbnail getThumbnail() {
             return null;
         }
     }
    }

现在业务结束

public void run(){

    request.getParameters().put("appid", "javasdktest");
   // request.setResults(15);


    do {
   while(!this.anyRequest){

    try{ 
    synchronized (this) {
     this.wait(); 
    }
    }catch(InterruptedException exp){
     ////Just Nothing
    }

   } 

         try{

          NewsPullParser parser = new NewsPullParser(RestClient.call(request.getRequestUrl(), request.getParameters()));
          ArrayList filter = new ArrayList();
          filter.add(NewsPullParser.TITLE);
          filter.add(NewsPullParser.SUMMARY);

          Bundle content = new Bundle(); 
          content.putSerializable("result",parser.parse(filter));
          Message msg = new Message();  
          msg.setData(content);
          this.callback.sendMessage(msg);   


         }catch(Exception exp){
       exp.printStackTrace();

         }


    this.anyRequest = false;
    this.lastRequest_finished = true;

  }while(!this.stopFlag);

 }