我正在处理一些我需要转换为平面文件的XML数据,因此我可以进行统计分析。我正在使用R分析数据。以下是数据样本:
<production xmlns="" diffgr:id="production1130" msdata:rowOrder="1129">
<ENTITY_ID>116484210</ENTITY_ID>
<LIQ>0</LIQ>
<GAS>163</GAS>
<WTR>0</WTR>
<WCNT>1</WCNT>
<DAYS>0</DAYS>
</production>
<production xmlns="" diffgr:id="production1131" msdata:rowOrder="1130">
<ENTITY_ID>116484210</ENTITY_ID>
<LIQ>12</LIQ>
<GAS>130</GAS>
<WTR>0</WTR>
<WCNT>1</WCNT>
<DAYS>0</DAYS>
</production>
我希望将其转换为如下所示的平面文件:
PRODUCTION_ID,ENTITY_ID,LIQ,GAS,WTR,WCNT,DAYS
有什么建议吗?
谢谢,Z
答案 0 :(得分:8)
简单示例:
install.packages("XML")
library("XML")
doc = xmlInternalTreeParse("/Users/ras/test.xml") # your path goes here
myframe = xmlToDataFrame(doc)
myframe
收率:
ENTITY_ID LIQ GAS WTR WCNT DAYS
1 116484210 0 163 0 1 0
2 116484210 12 130 0 1 0
test.xml是:
<stuff>
<production xmlns="" diffgr:id="production1130" msdata:rowOrder="1129">
<ENTITY_ID>116484210</ENTITY_ID>
<LIQ>0</LIQ>
<GAS>163</GAS>
<WTR>0</WTR>
<WCNT>1</WCNT>
<DAYS>0</DAYS>
</production>
<production xmlns="" diffgr:id="production1131" msdata:rowOrder="1130">
<ENTITY_ID>116484210</ENTITY_ID>
<LIQ>12</LIQ>
<GAS>130</GAS>
<WTR>0</WTR>
<WCNT>1</WCNT>
<DAYS>0</DAYS>
</production>
</stuff>