我正在尝试解析MLB GameDay中提供的信息。这是我正在使用的示例文件:
具体来说,这是一个示例XML输出:
<bottom>
<action b="0" s="0" o="0" des="Kole Calhoun remains in the game as the right fielder. " des_es="Kole Calhoun permanece en el juego como el jardinero derecho. " event="Defensive Switch" tfs="010840" tfs_zulu="2014-04-06T01:08:40Z" player="594777" pitch="5"/>
<atbat num="50" b="4" s="0" o="0" start_tfs="010846" start_tfs_zulu="2014-04-06T01:08:46Z" batter="514888" pitcher="572140" des="Jose Altuve walks. " des_es="Jose Altuve recibe base por bolas. " event="Walk" b1="514888" b2="" b3="">
<pitch sv_id="140405_200702" des="Ball" des_es="Bola mala" type="B" start_speed="90.6" pitch_type="FT"/>
<pitch sv_id="140405_200718" des="Ball" des_es="Bola mala" type="B" start_speed="90.8" pitch_type="FF"/>
<pitch sv_id="140405_200738" des="Ball" des_es="Bola mala" type="B" start_speed="91.5" pitch_type="FT"/>
<pitch sv_id="140405_200757" des="Ball" des_es="Bola mala" type="B" start_speed="90.2" pitch_type="FF"/>
</atbat>
<atbat num="51" b="1" s="1" o="2" start_tfs="011113" start_tfs_zulu="2014-04-06T01:11:13Z" batter="461882" pitcher="572140" des="Jesus Guzman grounds into a double play, third baseman John McDonald to second baseman Howie Kendrick to first baseman Albert Pujols. Jose Altuve out at 2nd. " des_es="Jesus Guzman batea rodado batea para doble matanza, tercera base John McDonald a segunda base Howie Kendrick a primera base Albert Pujols. Jose Altuve a cabo a 2da. " event="Grounded Into DP" b1="" b2="" b3="">
<pitch sv_id="140405_200849" des="Called Strike" des_es="Strike cantado" type="S" start_speed="90.2" pitch_type="FT"/>
<pitch sv_id="140405_200915" des="Ball" des_es="Bola mala" type="B" start_speed="74.0" pitch_type="CU"/>
<pitch sv_id="140405_200941" des="In play, out(s)" des_es="En juego, out(s)" type="X" start_speed="83.7" pitch_type="CH"/>
</atbat>
<atbat num="52" b="2" s="3" o="3" start_tfs="011242" start_tfs_zulu="2014-04-06T01:12:42Z" batter="474892" pitcher="572140" des="Chris Carter called out on strikes. " des_es="Chris Carter se poncha sin tirarle. " event="Strikeout" b1="" b2="" b3="">
<pitch sv_id="140405_201027" des="Ball" des_es="Bola mala" type="B" start_speed="73.5" pitch_type="CU"/>
<pitch sv_id="140405_201044" des="Ball" des_es="Bola mala" type="B" start_speed="91.2" pitch_type="FT"/>
<pitch sv_id="140405_201111" des="Called Strike" des_es="Strike cantado" type="S" start_speed="91.1" pitch_type="FT"/>
<pitch sv_id="140405_201132" des="Swinging Strike" des_es="Strike tirándole" type="S" start_speed="91.6" pitch_type="FT"/>
<pitch sv_id="140405_201155" des="Called Strike" des_es="Strike cantado" type="S" start_speed="92.5" pitch_type="FT"/>
</atbat>
</bottom>
我正在尝试解析<atbat>
标记后面的第一个兄弟<action>
标记。以下是我获取所需的所有<action>
标记的方法:
def set_bottom_actions
@xml_doc.elements.each("inning/bottom/action") { |element|
action = Action.new
action.init(element, @gid, @num)
@bottom_actions.push action
}
end
理想情况下,Action#init
会有一个名为atbat
的新初始化程序。问题是,如何获取first atbat tag
我实例化之后的action
?
在上面的例子中,如果我要抓住第一个action
,它还应该能够抓住下一个atbat
兄弟姐妹:
<atbat num="50" b="4" s="0" o="0" start_tfs="010846" start_tfs_zulu="2014-04-06T01:08:46Z" batter="514888" pitcher="572140" des="Jose Altuve walks. " des_es="Jose Altuve recibe base por bolas. " event="Walk" b1="514888" b2="" b3="">
答案 0 :(得分:1)
这是未经测试的(我使用并建议使用Nokogiri来解析XML),但是应该可以工作:
@xml_doc.elements.each("inning/bottom/action") { |element|
at_bat = REXML::XPath.first(elem, 'following-sibling::atbat[1]')
action = Action.new
action.init(element, at_bat, @gid, @num)
@bottom_actions.push action
}
# ...