R:将XML解析为所需的映射表

时间:2014-02-18 04:40:45

标签: xml r

我的XML格式如下,需要生成映射表。

<params>
     <parm suffix="1">A</parm>
     <parm suffix="2">B</parm>
     <parm suffix="3">C</parm>
     <parm suffix="4">D</parm>
     <parm suffix="5">E</parm>
 <params>

欲望输出是:parm值(A,B,C ......)映射到组合parm&amp;后缀(A_1,B_2,C_3 ......)

A -> A_1
B -> B_2
C -> C_3
D -> D_4
E -> E_5

任何类型的格式都可以,只要我能像parmList [“A”]一样检索A_1。 谢谢。

1 个答案:

答案 0 :(得分:1)

类似的东西:

library(XML)
xmltest <- "<params>
     <parm suffix=\"1\">A</parm>
     <parm suffix=\"2\">B</parm>
     <parm suffix=\"3\">C</parm>
     <parm suffix=\"4\">D</parm>
     <parm suffix=\"5\">E</parm>
</params>"

xmlout <- xmlInternalTreeParse(xmltest)
outattrs <- xpathApply(xmlout,"//params/parm",xmlGetAttr,"suffix")
outval <- xpathApply(xmlout,"//params/parm",xmlValue)
parmList <- setNames(Map(paste, outval, outattrs, sep="_"),outval)

#$A
#[1] "A_1"
#$B
#[1] "B_2"
#$C
#[1] "C_3"
#$D
#[1] "D_4"
#$E
#[1] "E_5"

parmList[["A"]]
#[1] "A_1"