代码之家  ›  专栏  ›  技术社区  ›  Chris

使用函数生成带有r的XML

  •  1
  • Chris  · 技术社区  · 6 年前

    我正在使用一个函数从R数据帧生成XML。

    xml  <- xmlTree()
    name <- 'Table1'
    
    convertToXML <- function(df, name) 
    { 
      xml$addTag('ObjectList', attrs=c(ObjectType="xxx"), close=FALSE)
      for (i in 1:nrow(df)) {
        xml$addTag(name, close=FALSE)
        for (j in names(df)) {
          xml$addTag(j, df[i, j])
        }
        xml$closeTag()
      }
      xml$closeTag()
      return(xml)
    }
    

    可以用这个变量吗 名称 在第一个标记的属性中?

    示例:如果 名称 为“Table1”,此行代码应如下所示:

      xml$addTag('ObjectList', attrs=c(ObjectType="Table1"), close=FALSE)
    

    我试图用paste()准备属性字符串,但是我没有正确地添加引号。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Spacedman    6 年前

    name 在表达式中,您将得到以下值:

     xml$addTag('ObjectList', attrs=c(ObjectType=name), close=FALSE)