R和XML - >导入数据文件

时间:2014-01-24 09:00:27

标签: xml r import

我想将一个XML文件放入R bij中,将XML文件分成三个数据帧。

我在下面添加了一部分XML文件。

我想要的是三个独立的数据帧,包含以下内容:

  1. 一个框架包含节点'Game'中的变量和变量值
  2. 一帧包含第一列中节点'Game'的变量'id'的值,而不是下一列中节点'Event'的变量
  3. 一个框架包含节点“Event”中的变量“id”,以及节点“Q”的值到下一列中。
  4. 这将导致一个小型数据库,我可以通过密钥的“id”链接数据。

    我一直在尝试使用XML包,但只是不让它工作。我认为对于有经验的XML用户来说这是相当简单的。

    以下是XML文件的一部分:

    <Games timestamp="2013-12-26T12:53:08">
    - <Game id="727429" away_team_id="123" away_team_name="Team A" competition_id="99" competition_name="Competition" game_date="2013-09-10" home_team_id="321" home_team_name="Team B" matchday="1" period_1_start="2012-09-10T19:00:42" period_2_start="2012-09-10T20:05:49" season_id="2009" season_name="Season 2009/2010">
    - <Event id="1750965992" event_id="1" type_id="34" period_id="16" min="0" sec="0" team_id="118" outcome="1" x="0.0" y="0.0" timestamp="2013-09-10T19:06:02.485" last_modified="2013-09-10T20:39:26">
      <Q id="1508752895" qualifier_id="227" value="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0" /> 
      <Q id="467627412" qualifier_id="59" value="12, 17, 19, 16, 2, 15, 8, 20, 14, 11, 10, 1, 3, 4, 5, 6, 7, 9, 18, 22, 23" /> 
      <Q id="1441747528" qualifier_id="44" value="1, 2, 2, 3, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5" /> 
      <Q id="454079800" qualifier_id="130" value="4" /> 
      <Q id="548551137" qualifier_id="194" value="5816" /> 
      <Q id="2130696856" qualifier_id="197" value="15" /> 
      <Q id="940400796" qualifier_id="30" value="12496, 18759, 38411, 19534, 18003, 17861, 5816, 19524, 17926, 49579, 17878, 4232, 88477, 28160, 77390, 12237, 17279, 42892, 52356, 17740, 8432" /> 
      <Q id="741889327" qualifier_id="131" value="1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0" /> 
      </Event>
    - <Event id="196028273" event_id="1" type_id="34" period_id="16" min="0" sec="0" team_id="831" outcome="1" x="0.0" y="0.0" timestamp="2013-09-10T19:16:53.985" last_modified="2013-09-10T20:54:02">
      <Q id="1972309909" qualifier_id="59" value="1, 4, 2, 18, 17, 3, 21, 16, 11, 7, 8, 12, 23, 25, 5, 6, 19, 24, 14, 15, 20, 22" /> 
      <Q id="584907211" qualifier_id="130" value="12" /> 
      <Q id="1500980037" qualifier_id="227" value="0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0" /> 
      <Q id="466076659" qualifier_id="44" value="1, 3, 3, 2, 2, 2, 3, 3, 4, 4, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5" /> 
      <Q id="867938921" qualifier_id="131" value="1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0" /> 
      <Q id="944778733" qualifier_id="194" value="33148" /> 
      <Q id="1374777290" qualifier_id="30" value="33148, 48852, 122779, 37269, 48853, 33324, 118749, 7459, 105835, 37265, 42565, 100566, 83425, 119534, 100981, 50902, 119613, 83725, 33176, 33298, 83531, 125510" /> 
      <Q id="525295255" qualifier_id="197" value="48" /> 
      </Event>
    - <Event id="1459266330" event_id="2" type_id="32" period_id="1" min="0" sec="0" team_id="118" outcome="1" x="0.0" y="0.0" timestamp="2013-09-10T19:00:42.327" last_modified="2013-09-10T19:00:42">
      <Q id="1757752982" qualifier_id="127" value="Left to Right" /> 
      </Event>
    - <Event id="444900728" event_id="2" type_id="32" period_id="1" min="0" sec="0" team_id="831" outcome="1" x="0.0" y="0.0" timestamp="2013-09-10T19:00:42.327" last_modified="2013-09-10T19:00:42">
      <Q id="884925323" qualifier_id="127" value="Right to Left" /> 
      </Event>
     </Game>
    </Games>
    

    我认为对于/ Event-node,这将是最合乎逻辑的:

    sp2 <- xmlToDataFrame(getNodeSet(doc, '//game/event'))
    

    但这就是我得到的:

    Error in `[<-.data.frame`(`*tmp*`, i, names(nodes[[i]]), value = c("",  : 
    duplicate subscripts for columns
    

    我是XML的绝对初学者,所以如果有人可以编写一些代码用于工作,我可以继续(学习和改进),这将非常有用!

1 个答案:

答案 0 :(得分:1)

这应该是一个好的开始。

library(XML)

doc <- htmlParse(your_file)
## 
game = xpathSApply(doc,'//game',xmlAttrs)
events = xpathSApply(doc,'//game/event',xmlAttrs)
questions = xpathSApply(doc,'//game/event/q',xmlAttrs)