2016-05-01 172 views
0

我试图用XML转换引入nokogiri到哈希:红宝石将引入nokogiri XML哈希

<eveapi version="2"> 
    <currentTime>2016-05-01 11:38:14</currentTime> 
    <result> 
    <characterID>93898118</characterID> 
    <characterName>Ghitzarai</characterName> 
    <race>Minmatar</race> 
    <bloodlineID>4</bloodlineID> 
    <bloodline>Brutor</bloodline> 
    <ancestryID>24</ancestryID> 
    <ancestry>Slave Child</ancestry> 
    <corporationID>98012663</corporationID> 
    <corporation>Dry Atomic Fusion</corporation> 
    </result> 
</eveapi> 

# asume xml is the above XML 
hash = {} 
xml.xpath('//result').each do |row| 
    hash[get_node_name:] = row.content 
end 

现在row.name将无法​​正常工作的原因,只有返回result一次。

如何从子节点获取正确的名称?

+0

我只是notised xml.xpath的lenght( '//结果')是onyl 1.所以也出现了问题是如何遍历孩子 –

+1

使用'... xpath('// result/*')...'? – har07

+0

啊,是的,工作。非常感谢! –

回答

2

“now row.name wont work because that only returns results result once。How to get the right names from the child nodes?”

添加/*result后得到<result>所有子元素不管孩子元素名称:

xml.xpath('//result/*').each do |row|