0

MLB GameDay で入手できる情報を解析しようとしています。以下は、私が使用しているファイルの例です。

http://gd2.mlb.com/components/game/mlb/year_2014/month_04/day_05/gid_2014_04_05_anamlb_houmlb_1/game_events.xml

具体的には、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="">
4

1 に答える 1

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
}
# ...
于 2014-06-05T18:01:48.367 に答える