xml - 在 R 中解析 XML

标签 xml r

我正在尝试从 XML 文件下载一些数据并将其组织到 R 中。我查看了相关问题和文档,但大多数都提到使用 xml 包解析函数,这似乎无法弄清楚我的数据。所以我的两个问题是 1) 如何让 R 将实际数据读入数据框而不是 XML 结构并进行标记?和 2) 我可以使用 XML 代码中的 XML 结构信息在 R 中解析它吗?

这就是我正在使用的(以及迄今为止我失败的尝试):

library("XML")
datahere = c("C:/.../R/feddata/data/")
setwd(datahere)

download.file("http://www.newyorkfed.org/markets/pomo/xml/v3_0/pomoXML.cfm?SHOWMORE=TRUE&date1=01/01/2009&date2=01/10/2009",paste(datahere,"feddata.xml",sep=""))

feddata = xmlInternalTreeParse("feddata.xml")
feddataset = xmlRoot(feddata)
names(feddata)
feddatanodeset = getNodeSet(feddata,"//DataSet/*")
feddatalist = xmlToList("feddata.xml")
feddatalist[2][1][1]
feddataDF = xmlToDataFrame(feddata)
feddata$doc$file$DataSet
feddata["doc"]["file"]["DataSet"]

feddataRoot = xmlRoot(feddata)
feddata2 = xmlSApply(feddata, function(x) xmlSApply(x,xmlValue))
feddata2 = xmlSApply(feddata$DataSet, function(x) xmlSApply(x,xmlValue))

非常感谢您的想法、建议、忠告等。如果您知道我可以在某处使用的另一个堆栈问题或教程或示例,那也可以,不想成为多余的。谢谢!

最佳答案

这是你想要的吗?

library(XML)
datahere = "~/"
setwd(datahere)
download.file("http://www.newyorkfed.org/markets/pomo/xml/v3_0/pomoXML.cfm?SHOWMORE=TRUE&date1=01/01/2009&date2=01/10/2009",paste(datahere,"feddata.xml",sep=""))
tt = xmlParse("feddata.xml")
out <- getNodeSet(tt, "//*[name()='out:issue']", fun=xmlToList)
df <- data.frame(do.call(rbind, out))
head(df)

  confidentiality couponRate currency decimals inclusion maturityDate multiplier rate securityType status
1               F      4.750      USD        0      true   2011-01-18          0    P          FMC      A
2               F      4.500      USD        0      true   2011-02-15          0    P          FNM      A
3               F      4.625      USD        0      true   2011-02-18          0    P          FHL      A
4               F      3.250      USD        0      true   2011-02-25          0    P          FMC      A
5               F      5.500      USD        0      true   2011-03-15          0    P          FNM      A
6               F      5.625      USD        0      true   2011-03-15          0    P          FMC      A
      value
1  40000000
2   3000000
3   2000000
4         0
5  49000000
6 155000000

关于xml - 在 R 中解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19747488/

相关文章:

xml - 根据子节点中的文本值删除父节点和所有子节点

r - 如何仅合并r中两个数据帧中满足特定条件的行

r - 如何使用整洁的求值语义来选择、复制和重命名 tibble 中的多个列?

iphone - KissXML 不返回字符串

Java XML importNode 函数未按预期工作

html - 在 R 中的 rvest 包中的 html 函数中使用哪个选择器?

javascript - R Shiny : conditionalPanel not working if used several widgets

c++ - R/为R编译c代码

regex - 使用 Notepad++ 中的正则表达式在xml标签中的字符串中查找特定单词?

xml - 在 XML 中存储引用数据的可接受方式是什么?