Я запускаю цикл else для итерации через XML-файл в ruby, присваивая значения хэшу. Есть 3 элемента в файле XML, но по какой-то причине он только итерации через первый, любая идея, почему?Else Loop работает только один раз
require "nokogiri"
f= File.open("untitled.xml")
doc = Nokogiri::XML(f)
f.close
doc.xpath('//item').each do |node|
children = node.children
item = {
"name" => node['name'],
"buyItNowPrice"=> children.css('buytItNowPrice').inner_text,
"description" => children.css('description').inner_text,
"startingBidPrice" => children.css('startingBidPrice').inner_text,
"closing_time" => children.css('closing_time').inner_text,
"closing_date" => children.css('closing_date').inner_text
}
puts item
end
XML:
<item name = "Test Thing">
<description>Something Coolest.</description>
<buytItNowPrice>154.99</buytItNowPrice>
<startingBidPrice>9999.99</startingBidPrice>
<closing_date>2014-12-25</closing_date>
<closing_time>12:32:PM</closing_time>
</item>
<item name = "Lazer">
<description>Something Cool.</description>
<buytItNowPrice>149.99</buytItNowPrice>
<startingBidPrice>9.99</startingBidPrice>
<closing_date>2014-12-25</closing_date>
<closing_time>12:32:PM</closing_time>
</item>
<item name = "Pokemon">
<description>Something even cooler.</description>
<buytItNowPrice>33.99</buytItNowPrice>
<startingBidPrice>9.99</startingBidPrice>
<closing_date>2014-12-25</closing_date>
<closing_time>12:32:PM</closing_time>
</item>
Выход только первый элемент печатается.
Просто удар в темноте, но, возможно, потому, что ваш XML недействителен? Попробуйте обернуть ваш '- ' 'в корневом узле'
' и прокрутить его. –
kasoban
Вот и все, спасибо! – user2816254
Я создал ответ из этого комментария, вы можете отметить его как принятое через несколько минут. Рад, что я могу помочь! – kasoban