首页 > 解决方案 > XML标记内的变量到R数据框中的列

问题描述

尝试将 XML 文件转换为 R 中的数据框。但是,对于一个客户,XML 标记(节点)具有相同的名称两次。我希望数据框中的每个节点都位于单独的列中。

<Consumer ConsumerOID="123">
<BillingContact>
        <PhoneInformation Type="Main">
        <PhoneNumber>4758796523</PhoneNumber>
      </PhoneInformation>
      <PhoneInformation Type="Home">
        <PhoneNumber>1234567890</PhoneNumber>
      </PhoneInformation>
</BillingContact>
</Consumer>
<Consumer ConsumerOID="256">
<BillingContact>
        <PhoneInformation Type="Main">
        <PhoneNumber>9856321475</PhoneNumber>
      </PhoneInformation>
</BillingContact>
</Consumer>
library(XML)

doc<-xmlParse("test.xml")
step1 <- xmlToDataFrame(nodes=getNodeSet(doc,"//OCADocument/Consumer/BillingContact/PhoneInformation"))

上面的代码只给了我电话号码,并且在数据框中的单个列中。这使得很难识别哪个电话号码属于哪个 consumerOID。

我想以如下所示的数据帧格式输出

"ConsumerID      Phonenumber1       Phonenumber2"
"  123           4758796523         1234567890"
"  256           9856321475         NA"

标签: rxmlxml2

解决方案


推荐阅读