Xml 在R中递归函数的data.frame中添加新行

Xml 在R中递归函数的data.frame中添加新行,xml,r,recursion,xml-parsing,dataframe,Xml,R,Recursion,Xml Parsing,Dataframe,我试图使用Duncan Temple Lang的XML包在R中解析XML。我的代码如下: library(XML) retrieveStructureInfo <- function(node, tableData) { tableD <- data.frame(path = NA, node = NA, value = NA) for (i in 1 : xmlSize(xmlAttrs(node))) { tableD <- rbind(t

我试图使用Duncan Temple Lang的XML包在R中解析XML。我的代码如下:

library(XML)
retrieveStructureInfo <- function(node, tableData) {  
    tableD <- data.frame(path = NA, node = NA, value = NA)

    for (i in 1 : xmlSize(xmlAttrs(node))) {
      tableD <- rbind(tableD, c("path", "node", "value"))  
      tableData <<- rbind(tableData, tableD)    
    }

    #children is the no. of nodes within a node
    for (i in 1 : children) {
      #recursive function call
      retrieveStructureInfo(node[[i]], tableD) 
    }
}

#parse xml document
#xmlfile is the file path
doc <- xmlParse(xmlfile)
r <- xmlRoot(doc)
tableData <- data.frame(path = NA, node = NA, value = NA)
retrieveStructureInfo(r, tableData)
tableData

通常,xml和递归的答案是使用xpath。您可以使用一些xpath查询或xmlToList之类的帮助函数创建一个表

x<- '<CATALOG>
   <PLANT>
      <COMMON Source="a" Available="false">Bloodroot</COMMON>
   </PLANT>
   <PLANT>
      <COMMON Source="b" Available="true">Columbine</COMMON>
   </PLANT>
</CATALOG>'

doc <- xmlParse(x)

xpathSApply(doc, "//COMMON", xmlValue)
[1] "Bloodroot" "Columbine"
xpathSApply(doc, "//COMMON", xmlGetAttr, "Source")
[1] "a" "b"

y <- xmlToList(doc)
data.frame(path=names(unlist(y)),value=unlist( y) )
                           path     value
1             PLANT.COMMON.text Bloodroot
2    PLANT.COMMON..attrs.Source         a
3 PLANT.COMMON..attrs.Available     false
4             PLANT.COMMON.text Columbine
5    PLANT.COMMON..attrs.Source         b
6 PLANT.COMMON..attrs.Available      true

library(plyr)
ldply(y, data.frame)  #OR 
ldply( y, function(x) data.frame(x, names(x$COMMON$.attrs)  ) )
    .id COMMON.text COMMON..attrs names.x.COMMON..attrs.
1 PLANT   Bloodroot             a                 Source
2 PLANT   Bloodroot         false              Available
3 PLANT   Columbine             b                 Source
4 PLANT   Columbine          true              Available

x谢谢你的回答,克里斯。据我所知,XPath需要指定路径。但我正在创建一个函数,该函数应该解析XML,而不管给它什么XML脚本。它应该像我的问题一样解析XML并生成输出。
                   path                 node                  value parent      type
  CATALOG/PLANT/COMMON               Source                    a    PLANT  attribute
  CATALOG/PLANT/COMMON            Available                  false  PLANT  attribute
  CATALOG/PLANT/COMMON               COMMON              Bloodroot  PLANT       text
x<- '<CATALOG>
   <PLANT>
      <COMMON Source="a" Available="false">Bloodroot</COMMON>
   </PLANT>
   <PLANT>
      <COMMON Source="b" Available="true">Columbine</COMMON>
   </PLANT>
</CATALOG>'

doc <- xmlParse(x)

xpathSApply(doc, "//COMMON", xmlValue)
[1] "Bloodroot" "Columbine"
xpathSApply(doc, "//COMMON", xmlGetAttr, "Source")
[1] "a" "b"

y <- xmlToList(doc)
data.frame(path=names(unlist(y)),value=unlist( y) )
                           path     value
1             PLANT.COMMON.text Bloodroot
2    PLANT.COMMON..attrs.Source         a
3 PLANT.COMMON..attrs.Available     false
4             PLANT.COMMON.text Columbine
5    PLANT.COMMON..attrs.Source         b
6 PLANT.COMMON..attrs.Available      true

library(plyr)
ldply(y, data.frame)  #OR 
ldply( y, function(x) data.frame(x, names(x$COMMON$.attrs)  ) )
    .id COMMON.text COMMON..attrs names.x.COMMON..attrs.
1 PLANT   Bloodroot             a                 Source
2 PLANT   Bloodroot         false              Available
3 PLANT   Columbine             b                 Source
4 PLANT   Columbine          true              Available