使用R中的重复元素名解析XML

时间:2015-02-11 23:06:21

标签: xml r

我正在尝试使用R解析Eve killog数据,我遇到了提取数据的问题,因为该文件在一次kill中重复使用相同的rowset标记。

这是XML的摘录(找到完整示例here):

  <rowset name="kills" key="killID" columns="killID,solarSystemID,killTime,moonID">
  <row killID="63" solarSystemID="30000848" killTime="2007-11-15 15:36:00" moonID="0">
    <victim characterID="150340823" characterName="Dieinafire" corporationID="1000169"
            corporationName="Center for Advanced Studies" allianceID="0"
            allianceName="" factionID="0" factionName=""
            damageTaken="6378" shipTypeID="12003" />
    <rowset name="attackers" columns="characterID,characterName,corporationID,corporationName,allianceID,allianceName,
            factionID,factionName,securityStatus,damageDone,finalBlow,weaponTypeID,shipTypeID">
      <row characterID="0" characterName="" corporationID="1000127" corporationName="Guristas"
           allianceID="0" allianceName="" factionID="0" factionName="" securityStatus="0" 
           damageDone="6313" finalBlow="1" weaponTypeID="0" shipTypeID="203" />
      <row characterID="0" characterName="" corporationID="150279367" corporationName="Starbase Anchoring Corp"
           allianceID="0" allianceName="" securityStatus="0" damageDone="65" finalBlow="0"
           weaponTypeID="0" shipTypeID="16632" />
    </rowset>

我的目标是为每次杀戮获得类似的东西:

killID               63
solarSystemID        30000848 
victim characterID   Dieinafire
attacker characterID ""
attacker characterID ""

我可以像这样提取受害者信息:

xpathSApply(xmlFile, "//victim", xmlGetAttr, 'characterName')

因为只有一个受害者,但我不知道如何提取攻击者信息。我试过这个:

xpathSApply(xmlFile, "//rowset/row/", xmlGetAttr, 'characterName')

但是我收到了很多空回复。有人可以建议一种干净的方法来为每次杀戮提取攻击者信息吗?

1 个答案:

答案 0 :(得分:3)

你可以尝试这个,我在整个XML上运行它,而不是摘录:

#finds nodes that have a `killID` attribute
data<-t(xpathSApply(xmlFile,"//row[@killID]",function(x){
  #gets the killI
  killID<-xpathSApply(x, ".", xmlGetAttr, 'killID')
  #gets the victimName
  victimName<-xpathSApply(x, "./victim", xmlGetAttr, 'characterName')
  #gets the attackers and pastes them together
  attackersId<-paste0(getNodeSet(x,"rowset[@name='attackers']/row/@characterID"),collapse=",")
  return(list(killID,victimName,attackersId))
}
))
colnames(data)<-c("KillID","victimName","attackersID")
data