2015-02-11 101 views
2

我試圖用R解析Eve killog數據,我遇到了解壓縮數據的問題,因爲該文件在一次kill內重複使用相同的rowset標記。用R中的重複元素名稱解析XML

這是從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') 

但我得到了很多空響應。有人可以建議一個乾淨的方式來提取每個殺人的攻擊者信息嗎?

+0

你得到了很多的空應答,因爲「characterName」很多屬性值都是空的。我不知道什麼是「攻擊者信息」。順便說一句,你發佈的XML代碼是壞的(你鏈接的是有效的)。 – djas 2015-02-12 00:35:35

回答

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 
+0

這就是我所需要的。謝謝! – Rilcon42 2015-02-12 00:14:32