如何将xml数据转换为R中的数据框 [英] How to convert xml data to data frame in R

查看:33
本文介绍了如何将xml数据转换为R中的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要在 R 中将一个 xml 文件加载到一个数据框中. xml 格式如下所示.我如何实现相同的目标?

Hello guys, I need to load an xml file into a data frame in R. The xml format is as shown below. How do I acheive the same?

         <?xml version="1.0" encoding="utf-8"?><posts>  <row Id="1" PostTypeId="1" AcceptedAnswerId="17" CreationDate="2010-07-26T19:14:18.907" Score="6"/></posts>

我尝试了下面的代码......它没有给出所需的输出.我期待列名及其值列在下面的表格输出.

I tried the below code....It does not give the desired output. I am expecting a tabular output with the column names and their values listed below.

library(XML)
xml.url ="test.xml"
xmlfile = xmlTreeParse(xml.url)

class(xmlfile)
xmltop=xmlRoot(xmlfile)

print(xmltop)[1:2]

plantcat <- xmlSApply(xmltop, function(x) xmlSApply(x, xmlValue))

plantcat_df <- data.frame(t(plantcat))

推荐答案

xml.text <- 
'<?xml version="1.0" encoding="utf-8"?>
<posts>  
<row Id="1" PostTypeId="1" AcceptedAnswerId="17" CreationDate="2010-07-26T19:14:18.907" Score="6"/>
<row Id="2" PostTypeId="1" AcceptedAnswerId="17" CreationDate="2010-07-26T19:14:18.907" Score="6"/>
<row Id="3" PostTypeId="1" AcceptedAnswerId="17" CreationDate="2010-07-26T19:14:18.907" Score="6"/>
<row Id="4" PostTypeId="1" AcceptedAnswerId="17" CreationDate="2010-07-26T19:14:18.907" Score="6"/>
</posts>'

library(XML)
xml <- xmlParse(xml.text)
result <- as.data.frame(t(xmlSApply(xml["/posts/row"],xmlAttrs)),
                        stringsAsFactors=FALSE)
#   Id PostTypeId AcceptedAnswerId            CreationDate Score
# 1  1          1               17 2010-07-26T19:14:18.907     6
# 2  2          1               17 2010-07-26T19:14:18.907     6
# 3  3          1               17 2010-07-26T19:14:18.907     6
# 4  4          1               17 2010-07-26T19:14:18.907     6

这比平常有点棘手,因为数据在属性中,而不是节点中(节点为空),所以我们不能使用 xlmToDataFrame(...) 不幸的是.

This is a bit trickier than usual because the data is in attributes, not nodes (the nodes are empty), so we can't use xlmToDataFrame(...) unfortunately.

以上所有数据仍然是字符,因此您仍然需要将列转换为任何合适的类.

All the data above is still character, so you still need to convert the columns to whatever class is appropriate.

这篇关于如何将xml数据转换为R中的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆