2014-01-07 31 views
0

我使用引入nokogiri解析这个XML:引入nokogiri和Rails - 小问题

<?xml version="1.0" encoding="utf-8"?> 
<GetPropertiesResponse> 
<Properties> 
    <Property> 
    <Id>19</Id> 
    <Name>Property 1</Name> 
    <Breakfast></Breakfast> 
    <Currency>GBP</Currency> 
    </Property> 

    <Property> 
    <Id>13</Id> 
    <Name>Property 2</Name> 
    <Breakfast>IN</Breakfast> 
    <Currency>EUR</Currency> 
    </Property> 

    <Property> 
    <Id>15</Id> 
    <Name>Property 3</Name> 
    <Breakfast>EX</Breakfast> 
    <Currency>GBP</Currency> 
    </Property> 

</Properties> 
</GetPropertiesResponse> 

这样的:

... 
doc = Nokogiri::XML(response.body) 
@property = doc.xpath("//Property") 

,然后在我看来,我显示每个属性的名称,像这样:

<%= @property.each do |item| %> 
    <%= item.xpath("Name").text %><br> 
<% end %> 

但我始终输出对最终神秘0,我不知道为什么?我的输出是这样的:

Property 1 
Property 2 
Property 3 
0 

回答

2

<%= @property.each do |item| %> 
    <%= item.xpath("Name").text %><br> 
<% end %> 

有一个额外的=标志

它应该是:

<% @property.each do |item| %> 
    <%= item.xpath("Name").text %><br> 
<% end %> 

的0是因为当@property.each完成返回0如果你删除=的返回值是未显示

+0

你是明星!欢呼声 - 当它让我时会打勾 – tommyd456