2012-03-28 90 views
0

嗨,我正在开发一个应用程序。我对android中的Web服务很陌生。我得到了服务器作为xml字符串的响应。我需要解析其内容以显示为列表视图。如何使用sax解析器来解析xml字符串。请提供一些代码。 anyhelp赞赏。如何解析从httppost作为字符串存储的响应

编辑:XML,我得到的字符串

<ns2:HotelListResponse xmlns:ns2 = "http://v3.hotel.wsapi.ean.com/"> 
<customerSessionId>0ABAA871-36B9-A891-3432-83874A39565C</customerSessionId> 
<numberOfRoomsRequested>1</numberOfRoomsRequested> 
<moreResultsAvailable>true</moreResultsAvailable> 
<cacheKey>e36b9a8:134383874a3:5660</cacheKey> 
<cacheLocation>10.186.168.113:7303</cacheLocation> 
<cachedSupplierResponse 
    matchedLocale="true" 
    matchedCurrency="true" 
    tpidUsed="5001" 
    otherOverheadTime="5" 
    candidatePreptime="71" 
    supplierResponseTime="9482" 
    supplierResponseNum="20" 
    supplierRequestNum="211" 
    cachedTime="0" /> 
<HotelList activePropertyCount = "237" size = "1"> 
    <HotelSummary ubsScore = "1801" order = "0"> 
     <hotelId>127092</hotelId> 
     <name>The Edgewater - A Noble House Hotel</name> 
     <address1>Pier 67, 2411 Alaskan Way</address1> 
     <city>Seattle</city> 
     <stateProvinceCode>WA</stateProvinceCode> 
     <postalCode>98121</postalCode> 
     <countryCode>US</countryCode> 
     <airportCode>SEA</airportCode> 
     <supplierType>E</supplierType> 
     <propertyCategory>1</propertyCategory> 
     <hotelRating>4.0</hotelRating> 
     <confidenceRating>85</confidenceRating> 
     <amenityMask>6259019</amenityMask> 
     <tripAdvisorRating>4.0</tripAdvisorRating> 
     <tripAdvisorReviewCount>590</tripAdvisorReviewCount> 
     <tripAdvisorRatingUrl>http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/4.0-12345-4.gif</tripAdvisorRatingUrl> 
     <locationDescription>Near Washington State Convention & Trade Center</locationDescription> 
     <shortDescription> 
      <b>Location.</b> 
      <br/> 
      The Edgewater - A Noble House Hotel is a business-friendly hotel located in central Seattle, close to Odyssey - The Maritime Discovery Center, Washington State Convention & 
     </shortDescription> 
     <highRate>249.0</highRate> 
     <lowRate>186.75</lowRate> 
     <rateCurrencyCode></rateCurrencyCode> 
     <latitude>47.61252</latitude> 
     <longitude>-122.35013</longitude> 
     <proximityDistance>11.898841</proximityDistance> 
     <proximityUnit>MI</proximityUnit> 
     <hotelInDestination>true</hotelInDestination> 
     <thumbNailUrl>/hotels/1000000/20000/11200/11133/11133_73_t.jpg</thumbNailUrl> 
     <deepLink>http://travel.ian.com/index.jsp?pageName=hotAvail&cid=55505&hotelID=127092&mode=2&numberOfRooms=1&room-0-adult-total=1&room-0-child-total=0&arrivalMonth=8&arrivalDay=3&departureMonth=8&departureDay=4&showInfo=true&locale=en_US&currencyCode=USD</deepLink> 
     <RoomRateDetailsList> 
      <RoomRateDetails> 
       <roomTypeCode>1160</roomTypeCode> 
       <rateCode>1221260</rateCode> 
       <maxRoomOccupancy>2</maxRoomOccupancy> 
       <quotedRoomOccupancy>2</quotedRoomOccupancy> 
       <minGuestAge>0</minGuestAge> 
       <roomDescription>City Lodge - Nonrefundable</roomDescription> 
       <promoId>200803780</promoId> 
       <promoDescription>7-Day Advance Purchase Special (Nonrefundable)</promoDescription> 
       <currentAllotment>10</currentAllotment> 
       <propertyAvailable>true</propertyAvailable> 
       <propertyRestricted>false</propertyRestricted> 
       <expediaPropertyId>11133</expediaPropertyId> 
       <rateKey>c9120acf-4428-4f1d-b6ca-d591651e7d93</rateKey> 
       <nonRefundable>true</nonRefundable> 
       <RateInfos size = "1"> 
        <RateInfo 
         rateChange = "false" 
         promo = "true" 
         priceBreakdown = "true"> 
         <RoomGroup> 
          <Room> 
           <numberOfAdults>1</numberOfAdults> 
           <numberOfChildren>0</numberOfChildren> 
          </Room> 
         </RoomGroup> 
         <ChargeableRateInfo 
          commissionableUsdTotal = "186.75" 
          total = "220.87" 
          surchargeTotal = "34.12" 
          nightlyRateTotal = "186.75" 
          averageBaseRate = "249.0" 
          averageRate = "186.75" 
          maxNightlyRate = "186.75" 
          currencyCode = "USD"> 
          <NightlyRatesPerRoom size = "1"> 
           <NightlyRate 
            promo = "true" 
            rate = "186.75" 
            baseRate = "249.0"/> 
          </NightlyRatesPerRoom> 
          <Surcharges size = "1"> 
           <Surcharge amount = "34.12" type = "TaxAndServiceFee"/> 
          </Surcharges> 
         </ChargeableRateInfo> 
         <ConvertedRateInfo 
          total = "220.87" 
          surchargeTotal = "34.12" 
          nightlyRateTotal = "186.75" 
          averageBaseRate = "249.0" 
          averageRate = "186.75" 
          maxNightlyRate = "186.75" 
          currencyCode = ""> 
          <NightlyRatesPerRoom size = "1"> 
           <NightlyRate 
            promo = "true" 
            rate = "186.75" 
            baseRate = "249.0"/> 
          </NightlyRatesPerRoom> 
          <Surcharges size = "1"> 
           <Surcharge amount = "34.12" type = "TaxAndServiceFee"/> 
          </Surcharges> 
         </ConvertedRateInfo> 
        </RateInfo> 
       </RateInfos> 
      </RoomRateDetails> 
     </RoomRateDetailsList> 
    </HotelSummary> 
</HotelList> 

+0

发表一些xml字符串的代码。 – 2012-03-28 16:10:58

回答

0

IBM已经发布了一个伟大的Tutorial其中详细解释了如何使用XML(尤其是SAX解析器)。 Google还为ApiDemos中的ListView提供了一些很棒的教程。所以,如果你基本上用SAX创建你的模型,并将它们放入你的ListView的BaseAdapter中,你应该没问题。

祝你好运, Tim