我正在处理返回 XML 的 Eve Online 请求。我正在为请求使用 HTTParty,并且我正在尝试使用 Nokogiri 来获取特定元素的属性值。
这是响应的示例:
<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')