0
我正在使用返回XML的Eve Online請求。我正在使用HTTParty進行請求,並且我正在嘗試使用Nokogiri來獲取特定元素的屬性值。Nokogiri HttParty Xpath Ruby on Rails
這裏的響應的例子:
<eveapi version="2"><currentTime>2012-10-19 22:41:56</currentTime><result><rowset name="transactions" key="refID" columns="date,refID,refTypeID,ownerName1,ownerID1,ownerName2,ownerID2,argName1,argID1,amount,balance,reason,taxReceiverID,taxAmount"><row date="2012-10-18 23:41:50" refID="232323" refTypeID="9" ownerName1="University of Caille" ownerID1="32232" ownerName2="name" ownerID2="34343" argName1="" argID1="0" amount="5000.00" balance="5000.00" reason="Starter fund" taxReceiverID="" taxAmount=""/></rowset></result><cachedUntil>2012-10-19 23:03:40</cachedUntil></eveapi>
我只需要訪問屬性的元素「行」和可以有很多行返回。
我讀了關於XPath的內容,據我所知,如果我做了以下操作,它應該返回所有行:doc.xpath('row')
但是,它不返回任何內容。
這是我到目前爲止有:
options = {:keyID => 111111, :vCode => 'fddfdfdfdf'}
response = HTTParty.post('https://api.eveonline.com/char/WalletJournal.xml.aspx', :body => options)
doc = Nokogiri::XML(response.body)
doc.xpath('row').each do |r|
end
循環永遠不會執行。
我在做什麼錯?我需要返回所有行元素並獲得對每個行屬性的訪問權限。
下面的代碼我收到了,這工作,直到我嘗試了不同的角色,並獲得了「不能轉換爲字符串錯誤整數」:
options = {:keyID => 434343, :vCode => 'fdfdfdfdf'}
response = HTTParty.post('https://api.eveonline.com/char/WalletJournal.xml.aspx', :body => options)
data = response.parsed_response
data['eveapi']['result']['rowset']['row'].each do |t|
if t['ownerName2'].eql?('name') && !t['reason'].eql?('Starter fund')
uniqueKey = t['ownerID1'].to_s + t['date']
if !Payment.exists?(:unique_payment_key => uniqueKey)
p = Payment.new
p.owner_id = t['ownerID1']
p.owner_name = t['ownerName1']
p.unique_payment_key = uniqueKey
p.amount = t['amount']
p.reason = t['reason']
p.save
end
end
end
對不起,錯誤的是error: can't convert String into Integer online line 42
這是
if t['ownerName2'].eql?('name') && !t['reason'].eql?('Starter fund')
好吧,我試圖使用nokogiri的原因是由於某種原因,我得到一個錯誤,當我嘗試循環它時返回散列。我認爲這是不能將整數轉換爲字符串。我讀過這是因爲數組索引。 – Brian
這真的很奇怪,因爲我原來的代碼可以在不同的Eve Online帳戶角色中正常工作,但是當我使用新角色時,我會收到該錯誤。 – Brian
也許你應該發佈錯誤和你正在使用的代碼。 –