如何使用sax解析器android解析字符串缓冲区中的数据

时间:2012-03-28 06:12:39

标签: android saxparser stringbuffer

我正在开发一个预订应用程序。我从服务器恢复了响应并将xml存储在stringbuffer中。如何从stringbuffer中检索/解析数据。请提供一些代码。 Stringbuffer是否有任何替代方法来存储数据。响应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>

编写代码以获取响应

  HttpResponse response = httpClient.execute(httpPost);
           System.out.println("response after fetch   "+response.toString());
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(response.getEntity()
                            .getContent(), "UTF-8"));
            StringBuffer responseString = new StringBuffer("");
            String line;
            while ((line = reader.readLine()) != null) {
                responseString.append(line);
            }

0 个答案:

没有答案