2017-02-07 76 views
1

這是我正在使用的XML文件的一個小例子。我想提取賣家評級大於150的拍賣數量。有誰知道我如何去做這件事?使用R從xml文件中提取多個值

<root> 
     <listing> 
      <seller_info> 
       <seller_name>seller12</seller_name> 
       <seller_rating>100</seller_rating> 
      </seller_info> 
      <payment_types> 
       Visa 
      </payment_types> 
      <shipping_info> 
       Buyer pays shipping charges. 
      </shipping_info> 
      <buyer_protection_info></buyer_protection_info> 
      <auction_info> 
       <current_bid>$820.00</current_bid> 
       <time_left>4 days, 18 hours +</time_left> 
       <high_bidder> 
        <bidder_name>[email protected]</bidder_name> 
        <bidder_rating>-2</bidder_rating> 
       </high_bidder> 
       <num_items>1</num_items> 
       <num_bids>12</num_bids> 
       <started_at>$1.00</started_at> 
       <bid_increment></bid_increment> 
       <notes></notes> 
      </auction_info> 
     </listing> 
     <listing> 
      <seller_info> 
       <seller_name>seller50</seller_name> 
       <seller_rating>200</seller_rating> 
      </seller_info> 
      <payment_types> 
       Visa 
      </payment_types> 
      <shipping_info> 
       Buyer pays shipping charges. 
      </shipping_info> 
      <buyer_protection_info></buyer_protection_info> 
      <auction_info> 
       <current_bid>$920.00</current_bid> 
       <time_left>4 days, 17 hours +</time_left> 
       <high_bidder> 
        <bidder_name>[email protected]</bidder_name> 
        <bidder_rating>-2</bidder_rating> 
       </high_bidder> 
       <num_items>1</num_items> 
       <num_bids>5</num_bids> 
       <started_at>$1.00</started_at> 
       <bid_increment></bid_increment> 
       <notes></notes> 
      </auction_info> 
     </listing> 
<root> 

到目前爲止,我已經使用解析這個xmlTreeParse數據和使用xpathSapply

doc <- xmlTreeParse("ebay.xml", useInternalNodes = TRUE) 
log <- xpathSApply(doc, '//*/seller_rating') 
+0

那麼,什麼是你試圖代碼的問題?什麼是期望的結果? – Parfait

+0

@Parfait問題是我提取賣家評級的所有拍賣,而不僅僅是賣家評級大於150的拍賣。我只想輸出賣家評級> 150的拍賣數量 – Amanda

回答

2

我看到你的代碼也越來越標籤。如果你使用:

SellerRatings = xmlSApply(doc["//listing//seller_info//seller_rating"], xmlValue) 

你只會得到值,所以你可以數它們。

sum(SellerRatings > 150) 

或簡稱

sum(xmlSApply(doc["//*//seller_rating"], xmlValue) > 150) 
+0

Thanks @ G5W。當我嘗試你的代碼時,我得到這個錯誤:'UseMethod(「xpathApply」)錯誤: 沒有適用於'xpathApply'的方法應用於類「XMLNodeSet」的對象 – Amanda

+0

它是一個S – G5W

+0

奇怪的,我正在使用S.' doc < - xmlTreeParse(「ebay.xml」,useInternalNodes = TRUE) log < - xpathSApply(doc [「// listing // seller_info // seller_rating」],xmlValue)' – Amanda