目前,我尝试通过YQL获取数据。
select * from yahoo.finance.quotes where symbol in ("BP")
我会得到XML响应。
我想知道,在我的Java应用程序中,是否有可用于解析XML的常用库?
或者我应该要求回复是否采用JSON格式?然后使用JSONObject来解析它?哪一个会更容易?
这是XML响应。
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
yahoo:count="1" yahoo:created="2010-09-16T05:00:13Z" yahoo:lang="en-US">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<url execution-time="2" proxy="DEFAULT"><![CDATA[http://datatables.org/alltables.env]]></url>
<url execution-time="2" proxy="DEFAULT"><![CDATA[http://www.datatables.org/yahoo/finance/yahoo.finance.quotes.xml]]></url>
<url execution-time="9" proxy="DEFAULT"><![CDATA[http://download.finance.yahoo.com/d/quotes.csv?f=aa2bb2b3b4cc1c3c6c8dd1d2ee1e7e8e9ghjkg1g3g4g5g6ii5j1j3j4j5j6k1k2k4k5ll1l2l3mm2m3m4m5m6m7m8nn4opp1p2p5p6qrr1r2r5r6r7ss1s7t1t7t8vv1v7ww1w4xy&s=BP]]></url>
<url execution-time="11"><![CDATA[select * from csv where url=@url and columns='Ask,AverageDailyVolume,Bid,AskRealtime,BidRealtime,BookValue,Change&PercentChange,Change,Commission,ChangeRealtime,AfterHoursChangeRealtime,DividendShare,LastTradeDate,TradeDate,EarningsShare,ErrorIndicationreturnedforsymbolchangedinvalid,EPSEstimateCurrentYear,EPSEstimateNextYear,EPSEstimateNextQuarter,DaysLow,DaysHigh,YearLow,YearHigh,HoldingsGainPercent,AnnualizedGain,HoldingsGain,HoldingsGainPercentRealtime,HoldingsGainRealtime,MoreInfo,OrderBookRealtime,MarketCapitalization,MarketCapRealtime,EBITDA,ChangeFromYearLow,PercentChangeFromYearLow,LastTradeRealtimeWithTime,ChangePercentRealtime,ChangeFromYearHigh,PercebtChangeFromYearHigh,LastTradeWithTime,LastTradePriceOnly,HighLimit,LowLimit,DaysRange,DaysRangeRealtime,FiftydayMovingAverage,TwoHundreddayMovingAverage,ChangeFromTwoHundreddayMovingAverage,PercentChangeFromTwoHundreddayMovingAverage,ChangeFromFiftydayMovingAverage,PercentChangeFromFiftydayMovingAverage,Name,Notes,Open,PreviousClose,PricePaid,ChangeinPercent,PriceSales,PriceBook,ExDividendDate,PERatio,DividendPayDate,PERatioRealtime,PEGRatio,PriceEPSEstimateCurrentYear,PriceEPSEstimateNextYear,Symbol,SharesOwned,ShortRatio,LastTradeTime,TickerTrend,OneyrTargetPrice,Volume,HoldingsValue,HoldingsValueRealtime,YearRange,DaysValueChange,DaysValueChangeRealtime,StockExchange,DividendYield']]></url>
<javascript execution-time="26" instructions-used="67513" table-name="yahoo.finance.quotes"/>
<user-time>48</user-time>
<service-time>13</service-time>
<build-version>9128</build-version>
</diagnostics>
<results>
<quote symbol="BP">
<Ask/>
<AverageDailyVolume>38597700</AverageDailyVolume>
<Bid/>
<AskRealtime>38.25</AskRealtime>
<BidRealtime>38.15</BidRealtime>
<BookValue>27.294</BookValue>
<Change_PercentChange>-0.34 - -0.88%</Change_PercentChange>
<Change>-0.34</Change>
<Commission/>
<ChangeRealtime>-0.34</ChangeRealtime>
<AfterHoursChangeRealtime>N/A - N/A</AfterHoursChangeRealtime>
<DividendShare>2.52</DividendShare>
<LastTradeDate>9/15/2010</LastTradeDate>
<TradeDate/>
<EarningsShare>-0.462</EarningsShare>
<ErrorIndicationreturnedforsymbolchangedinvalid>N/A</ErrorIndicationreturnedforsymbolchangedinvalid>
<EPSEstimateCurrentYear>6.14</EPSEstimateCurrentYear>
<EPSEstimateNextYear>6.38</EPSEstimateNextYear>
<EPSEstimateNextQuarter>1.38</EPSEstimateNextQuarter>
<DaysLow>37.77</DaysLow>
<DaysHigh>38.25</DaysHigh>
<YearLow>26.75</YearLow>
<YearHigh>62.38</YearHigh>
<HoldingsGainPercent>- - -</HoldingsGainPercent>
<AnnualizedGain>-</AnnualizedGain>
<HoldingsGain/>
<HoldingsGainPercentRealtime>N/A - N/A</HoldingsGainPercentRealtime>
<HoldingsGainRealtime/>
<MoreInfo>cnprmIed</MoreInfo>
<OrderBookRealtime>N/A</OrderBookRealtime>
<MarketCapitalization>119.6B</MarketCapitalization>
<MarketCapRealtime/>
<EBITDA>7.272B</EBITDA>
<ChangeFromYearLow>+11.43</ChangeFromYearLow>
<PercentChangeFromYearLow>+42.73%</PercentChangeFromYearLow>
<LastTradeRealtimeWithTime>N/A - <b>38.18</b></LastTradeRealtimeWithTime>
<ChangePercentRealtime>N/A - -0.88%</ChangePercentRealtime>
<ChangeFromYearHigh>-24.20</ChangeFromYearHigh>
<PercebtChangeFromYearHigh>-38.79%</PercebtChangeFromYearHigh>
<LastTradeWithTime>Sep 15 - <b>38.18</b></LastTradeWithTime>
<LastTradePriceOnly>38.18</LastTradePriceOnly>
<HighLimit/>
<LowLimit/>
<DaysRange>37.77 - 38.25</DaysRange>
<DaysRangeRealtime>N/A - N/A</DaysRangeRealtime>
<FiftydayMovingAverage>37.8139</FiftydayMovingAverage>
<TwoHundreddayMovingAverage>44.0511</TwoHundreddayMovingAverage>
<ChangeFromTwoHundreddayMovingAverage>-5.8711</ChangeFromTwoHundreddayMovingAverage>
<PercentChangeFromTwoHundreddayMovingAverage>-13.33%</PercentChangeFromTwoHundreddayMovingAverage>
<ChangeFromFiftydayMovingAverage>+0.3661</ChangeFromFiftydayMovingAverage>
<PercentChangeFromFiftydayMovingAverage>+0.97%</PercentChangeFromFiftydayMovingAverage>
<Name>BP p.l.c. Common</Name>
<Notes>-</Notes>
<Open>38.08</Open>
<PreviousClose>38.52</PreviousClose>
<PricePaid/>
<ChangeinPercent>-0.88%</ChangeinPercent>
<PriceSales>0.42</PriceSales>
<PriceBook>1.41</PriceBook>
<ExDividendDate>May 5</ExDividendDate>
<PERatio/>
<DividendPayDate>Jun 21</DividendPayDate>
<PERatioRealtime/>
<PEGRatio>1.25</PEGRatio>
<PriceEPSEstimateCurrentYear>6.27</PriceEPSEstimateCurrentYear>
<PriceEPSEstimateNextYear>6.04</PriceEPSEstimateNextYear>
<Symbol>BP</Symbol>
<SharesOwned/>
<ShortRatio>0.80</ShortRatio>
<LastTradeTime>4:00pm</LastTradeTime>
<TickerTrend>&nbsp;+-+=+-&nbsp;</TickerTrend>
<OneyrTargetPrice>47.27</OneyrTargetPrice>
<Volume>6831680</Volume>
<HoldingsValue/>
<HoldingsValueRealtime/>
<YearRange>26.75 - 62.38</YearRange>
<DaysValueChange>- - -0.88%</DaysValueChange>
<DaysValueChangeRealtime>N/A - N/A</DaysValueChangeRealtime>
<StockExchange>NYSE</StockExchange>
<DividendYield>6.54</DividendYield>
<PercentChange>-0.88%</PercentChange>
</quote>
</results>
</query>
答案 0 :(得分:1)
我认为Yahoo不提供Java API。您必须create an XML schema,然后使用XML库来创建实体类(JAXB或类似的)。也可以用JSON获取YQL数据,在这种情况下我会使用Google GSON parser并手动创建实体类。