将XML转换为数据帧 [英] Transform XML into a data frame

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

问题描述

我正在寻找一种简单有效的方式来将XML数据转换为数据框架(但不是所有元素)。



我有这个文件: http://www-sop.inria.fr/成员/ Philippe.Poulard / projet / 2013 / entries_hotels.xml



我使用 xpathSApply ,但这是坏的,因为它不节省null元素。
在文件中,一些纬度是空的,但是 xpathSApply 我不知道哪些酒店被忽略了空的纬度元素。



我发现了 xmlToList 函数,它与XML很好,因为它具有相同的结构(避免在一个数据框)。



但现在我有两个问题:



如果我要创建一个数据框这个列表有一个耗尽的元素列表,并保持NULLs元素,我该怎么办?
我这样做,但是NULL不保存在我的向量中:

 库(XML)
酒店< - http://www-sop.inria.fr/members/Philippe.Poulard/projet/2013/entries_hotels.xml
list< - xmlToList(hotels)
latitudes.hotels< ; - c()
(列表中的元素){latitudes.hotels < - c(latitudes.hotels,element $ latitude)}

我的第二个问题是,如果我想直接与我的列表一起工作,问题是所有的名称都是sames:entry。

然后我想知道如何使用Id等于x的条目,例如 which(list $ entry $ ID == x)

我可以用与上述相同类型的向量来执行

  ids.hotels<  -  c()
for(元素在列表中){ids.hotels< - c(ids.hotels,元素$ ID)}
list [[which(ids.hotels == x)]]

但我认为有更好的方法来做,如果一个ID元素在我的XML文件中为空,那就是错误的。 >

感谢您的帮助

解决方案

我不熟悉XML包,但是您可以使用基本功能提取元素,并且可以保留缺少的经度/纬度。

  lst < -  xmlToList(hotels) 

ll< - lapply(1:150,function(z)
c(id = lst [[z]] [['ID']],name = lst [[z] ] [['name_fr']],
lat = lst [[z]] [['latitude]],long = lst [[z]] [['longitude]]))

库(plyr)
df< - rbind.fill(
lapply(ll,function(y){as.data.frame(t(y),stringsAsFactors = FALSE)}) )

从这里获得rbind.fill:do.call(rbind,列表)列数不一致



另外,列表的所有名称都是条目,例如使用第一个名称(lst [1]),您可以通过名称获取名称(lst [[1]])


I'm looking for a simple and efficient way to transform XML datas as a data.frame (but not all the elements though).

I have this file : http://www-sop.inria.fr/members/Philippe.Poulard/projet/2013/entries_hotels.xml

I used xpathSApply, but that's bad because it doesn't conserve the null elements. In the file some latitudes are empty, but with xpathSApply I can't know which hotels have an empty latitude element because they are ignored.

I found the xmlToList function, and it's nice with XML because it's prety the same structure (it avoid to have many NULL values in a data frame).

But now I have 2 problems :

If I want to create a data frame from this list with an exhausting list of elements and keep the NULLs elements, how can i do ? I did this but NULLs aren't kept in my vector :

library(XML)
hotels <- "http://www-sop.inria.fr/members/Philippe.Poulard/projet/2013/entries_hotels.xml"
list <- xmlToList(hotels)
latitudes.hotels <- c()
for(element in list) {latitudes.hotels <- c(latitudes.hotels, element$latitude)}

And my second problem is that if I want to work directly with my list, the problem is that all the names are the sames : "entry".
Then I wonder how I can acces to the entry with the Id equals to x for example, which(list$entry$ID == x).
I can do it with the same type of vector than above

ids.hotels <- c()
for(element in list) {ids.hotels <- c(ids.hotels, element$ID)}
list[[which(ids.hotels == x)]]

But I think there is a better way to do it, and it's wrong if one ID element is empty in my XML file.

Thank you for any help

解决方案

I'm not familiar with the XML package, however you can extract elements using base functions and can retain the missing longitude/latitude.

lst <- xmlToList(hotels)

ll <- lapply(1:150 , function(z) 
                c(id=lst[[z]][['ID']],name=lst[[z]][['name_fr']],
                lat=lst[[z]][['latitude']],long=lst[[z]][['longitude']]))

library(plyr)
df <- rbind.fill(
            lapply(ll,function(y){as.data.frame(t(y),stringsAsFactors=FALSE)}))

Got the rbind.fill from here: do.call(rbind, list) for uneven number of column

Also whereas all the names of the list are 'entry' eg using names(lst[1]) for the first, you can get the names by names(lst[[1]])

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

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