在r中的xml文件中提取部分内容

时间:2018-03-01 14:37:38

标签: r xml extract

有没有办法从R中的XML文件中提取:businessUnit,isinCond,otrVol,otrNo的内容?

这是我尝试的代码,但它没有提供任何内容@@

library("XML")
library("methods")
result <- xmlParse("N:/HFT/Test1/51RPTTR101XETRA20180228XETRT7.XML")
rootnode <- xmlRoot(result)
xpathSApply(result, "//*/ns:businessUnitGrp/ns:businessUnit"
        , namespaces = c(ns = "tr101Grp1")
        , xmlValue)

<participantGrp>
  <participant>ACAMU</participant>
  <partLngName>ACON ACTIENBANK AG</partLngName>
</participantGrp>

  

<tr101KeyGrp1>
  <businessUnitGrp>
    <businessUnit>ACAMU</businessUnit>
    <busUntLngName>ACON ACTIENBANK AG</busUntLngName>
    <businessUnitId>12641</businessUnitId>
  </businessUnitGrp>
  <actTradeDay>20</actTradeDay>
  <numTradeDays>20</numTradeDays>
</tr101KeyGrp1>
<tr101Grp2>
  <tr101KeyGrp2>
    <otrMktGrp>Europe</otrMktGrp>
    <isinCod>CH0132594711</isinCod>
    <otrVol>5.9225</otrVol>
    <otrNo>5.5455</otrNo>
    <violation>0</violation>
    <maxRatioVol>20000000</maxRatioVol>
    <maxRatioNo>5000000</maxRatioNo>
    <floorVol>10000</floorVol>
    <floorNo>1</floorNo>
  </tr101KeyGrp2>

1 个答案:

答案 0 :(得分:0)

我不确定你想做什么,但你的XML似乎格式错误,或者示例不完整。

以下是您的XML示例。我添加了一个根节点和</tr101Grp2>以使其工作:

xmlstring <- "<data>
<tr101KeyGrp1>
  <businessUnitGrp>
<businessUnit>ACAMU</businessUnit>
<busUntLngName>ACON ACTIENBANK AG</busUntLngName>
<businessUnitId>12641</businessUnitId>
</businessUnitGrp>
<actTradeDay>20</actTradeDay>
<numTradeDays>20</numTradeDays>
</tr101KeyGrp1>
<tr101Grp2>
<tr101KeyGrp2>
<otrMktGrp>Europe</otrMktGrp>
<isinCod>CH0132594711</isinCod>
<otrVol>5.9225</otrVol>
<otrNo>5.5455</otrNo>
<violation>0</violation>
<maxRatioVol>20000000</maxRatioVol>
<maxRatioNo>5000000</maxRatioNo>
<floorVol>10000</floorVol>
<floorNo>1</floorNo>
</tr101KeyGrp2>
</tr101Grp2>
</data>"

library(XML)

result <- xmlParse(xmlstring)

rootnode <- xmlRoot(result)
xpathSApply(result, "//*/businessUnitGrp/businessUnit"
            , namespaces = c(ns = "tr101Grp1")
            , xmlValue)

给出了:

[1] "ACAMU"