解析xml响应致命错误

时间:2014-10-24 07:26:33

标签: xml parsing

格式化XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns2:HotelListResponse xmlns:ns2="http://v3.hotel.wsapi.ean.com/">
   <customerSessionId>0ABAAA86-43C0-8659-1492-40BA6A6C9ED0</customerSessionId>
   <numberOfRoomsRequested>1</numberOfRoomsRequested>
   <moreResultsAvailable>true</moreResultsAvailable>
   <cacheKey>-643c0865:14940ba6a6c:ed1</cacheKey>
   <cacheLocation>10.186.170.134:7300</cacheLocation>
   <cachedSupplierResponse supplierCacheTolerance="NOT_SUPPORTED" cachedTime="0" supplierRequestNum="226" supplierResponseNum="25" supplierResponseTime="808" candidatePreptime="9" otherOverheadTime="17" tpidUsed="5001" matchedCurrency="true" matchedLocale="true" />
   <HotelList size="25" activePropertyCount="246">
      <HotelSummary order="0" ubsScore="1077510">
         <hotelId>164116</hotelId>
         <name>Sheraton Seattle Hotel</name>
         <address1>1400 6th Ave</address1>
         <city>Seattle</city>
         <stateProvinceCode>WA</stateProvinceCode>
         <postalCode>98101</postalCode>
         <countryCode>US</countryCode>
         <airportCode>SEA</airportCode>
         <supplierType>E</supplierType>
         <propertyCategory>1</propertyCategory>
         <hotelRating>4.0</hotelRating>
         <confidenceRating>30</confidenceRating>
         <amenityMask>262147</amenityMask>
         <tripAdvisorRating>4.0</tripAdvisorRating>
         <tripAdvisorReviewCount>2074</tripAdvisorReviewCount>
         <tripAdvisorRatingUrl>http://www.tripadvisor.com/img/cdsi/img2/ratings/traveler/4.0-12345-4.gif</tripAdvisorRatingUrl>
         <locationDescription>Near Pike Place Market</locationDescription>
         <shortDescription>&amp;lt;p&amp;gt;&amp;lt;b&amp;gt;Property Location&amp;lt;/b&amp;gt; &amp;lt;br /&amp;gt;With a stay at Sheraton Seattle Hotel, you&amp;apos;ll be centrally located in Seattle, steps from 5th Avenue Theater and Washington State Convention Center. This 4-star</shortDescription>
         <highRate>145.8</highRate>
         <lowRate>145.8</lowRate>
         <rateCurrencyCode>USD</rateCurrencyCode>
         <latitude>47.61017</latitude>
         <longitude>-122.33357</longitude>
         <proximityDistance>11.115907</proximityDistance>
         <proximityUnit>MI</proximityUnit>
         <hotelInDestination>true</hotelInDestination>
         <thumbNailUrl>/hotels/1000000/30000/25300/25290/25290_88_t.jpg</thumbNailUrl>

         <RoomRateDetailsList>
            <RoomRateDetails>
               <roomTypeCode>200419433</roomTypeCode>
               <rateCode>202558544</rateCode>
               <maxRoomOccupancy>4</maxRoomOccupancy>
               <quotedRoomOccupancy>2</quotedRoomOccupancy>
               <minGuestAge>1</minGuestAge>
               <roomDescription>Traditional Room</roomDescription>
               <propertyAvailable>true</propertyAvailable>
               <propertyRestricted>false</propertyRestricted>
               <expediaPropertyId>25290</expediaPropertyId>
               <RateInfos size="1">
                  <RateInfo priceBreakdown="true" promo="true" rateChange="false">
                     <RoomGroup>
                        <Room>
                           <numberOfAdults>2</numberOfAdults>
                           <numberOfChildren>0</numberOfChildren>
                        </Room>
                     </RoomGroup>
                   <ChargeableRateInfo averageBaseRate="145.8" averageRate="145.8" commissionableUsdTotal="291.6" currencyCode="USD" maxNightlyRate="145.8"        nightlyRateTotal="291.6" grossProfitOffline="35.27" grossProfitOnline="58.82" surchargeTotal="57.34" total="348.94">
                        <NightlyRatesPerRoom size="2">
                           <NightlyRate baseRate="145.8" rate="145.8" promo="false" />
                           <NightlyRate baseRate="145.8" rate="145.8" promo="false" />
                        </NightlyRatesPerRoom>
                        <Surcharges size="1">
                           <Surcharge type="TaxAndServiceFee" amount="57.34" />
                        </Surcharges>
                     </ChargeableRateInfo>
                     <nonRefundable>true</nonRefundable>
                     <rateType>MerchantStandard</rateType>
                     <promoId>205881880</promoId>
                     <promoDescription>Sale! Save 10% on this Stay.</promoDescription>
                     <promoType>Standard</promoType>
                     <currentAllotment>678</currentAllotment>
                  </RateInfo>
               </RateInfos>
            </RoomRateDetails>
         </RoomRateDetailsList>
      </HotelSummary>

该错误给我一个[致命错误]:行开头的prolog中不允许1:1:1的内容。

假设我只想获得酒店的名称和价格,有人可以帮助我在开始时解决错误吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

&#34; prolog中不允许的内容&#34;可以有多种原因,包括:

  • 在XML声明之前有一些东西,例如:换行符

  • 文件以BOM(字节顺序标记)开头,您使用的是无法识别BOM的XML解析器

  • 您正在解析的文件不是您认为要解析的文件:检查您正在使用的API