我有这个xml
<league xmlns="http://feed.elasticstats.com/schema/basketball/schedule-v2.0.xsd" id="4353138d-4c22-4396-95d8-5f587d2df25c" name="NBA" alias="NBA">
<season-schedule id="eb322501-7291-457b-9bbb-b4106ecf9564" year="2014" type="PRE">
<games>
...
如何访问'season-schedule'的内容
到目前为止,我已经尝试过这些但没有取得任何成功:
doc2.html.body.league.seasonschedule
NoMethodError: undefined method `seasonschedule' for #<Nokogiri::XML::Element:0x007faddf5ce2b8>
from /Users/boti/.rvm/gems/ruby-2.0.0-p353@dime/gems/nokogiri-1.6.3.1/lib/nokogiri/decorators/slop.rb:30:in `method_missing'
[13] pry(main)> doc2.html.body.league.season_schedule
NoMethodError: undefined method `season_schedule' for #<Nokogiri::XML::Element:0x007faddf5ce2b8>
from /Users/boti/.rvm/gems/ruby-2.0.0-p353@dime/gems/nokogiri-1.6.3.1/lib/nokogiri/decorators/slop.rb:30:in `method_missing'
答案 0 :(得分:1)
不幸的是,似乎没有比以下更好的方式了:
doc2.html.body.league.send('season-schedule')
另请阅读Nokogiri docs中对坡度部分的评论。那些是
答案 1 :(得分:1)
我不确定问题是什么,因为这有效:
require 'nokogiri'
doc = Nokogiri::HTML(<<EOT)
<league xmlns="http://feed.elasticstats.com/schema/basketball/schedule-v2.0.xsd" id="4353138d-4c22-4396-95d8-5f587d2df25c" name="NBA" alias="NBA">
<season-schedule id="eb322501-7291-457b-9bbb-b4106ecf9564" year="2014" type="PRE">
<games/>
</season-schedule>
</league>
EOT
doc.at('season-schedule') # => #<Nokogiri::XML::Element:0x3fefd20679d8 name="season-schedule" attributes=[#<Nokogiri::XML::Attr:0x3fefd20678e8 name="id" value="eb322501-7291-457b-9bbb-b4106ecf9564">, #<Nokogiri::XML::Attr:0x3fefd20678d4 name="year" value="2014">, #<Nokogiri::XML::Attr:0x3fefd20678c0 name="type" value="PRE">] children=[#<Nokogiri::XML::Text:0x3fefd2067014 "\n ">, #<Nokogiri::XML::Element:0x3fefd2066f60 name="games">, #<Nokogiri::XML::Text:0x3fefd2066d94 "\n ">]>
doc.at('season-schedule')['id'] # => "eb322501-7291-457b-9bbb-b4106ecf9564"