[R] Convert data frame to XML-Tree

Dieter Menne dieter.menne at menne-biomed.de
Fri Feb 12 11:18:07 CET 2010

T.Wunder wrote:
>>> Well, now I get a warning message like
>>> "In xmlRoot.XMLInternalDocument(currentNodes[[1]]) : empty XML document"
>>> if I use this function. How could this be fixed?
>> By telling use how you "used" that function.
> I'm sorry. I have a data frame df and typed
> convertToXML(df,"Tabelle")

If you use that function, you first get an error message that XML is unknown
because you did not tell us that you used library(XML). It's the best guess
here, but in many cases there are several libraries (pardon, Martin,
packages) that do the same.

Then it tells us that df is unknown, because you did not tell us what the
data frame was. It could be a problem in your df, for example some
unexpected data.

So let's try a self-contained example with the iris data set available in
all installations. It shows that everything works Ok, with the exception of
the warning. By converting it to an error, your can locate the warning at
the line xmlTree.

The warning can be simply reproduced by 


library(XML) # or did you use a different package?
convertToXML <- function(df,name)
  xml <- xmlTree("Test")
  xml$addNode(name, close=FALSE)
  for (i in 1:nrow(df)) {
       xml$addNode("value", close=FALSE)
       for (j in names(df)) {
           xml$addNode(j, df[i, j])

data(iris) # these data are always there
tr = convertToXML(iris,"Tabelle") 
cat(saveXML(tr$value())) ## looks good
options(warn=2) # to find where the warning occured
cat(saveXML(tr$value())) ## looks good

## Looks like it is no problem of the function, because the following line
# shows the same error
# Report it to Duncan 

R version 2.10.1 (2009-12-14) 

[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] XML_2.6-0

loaded via a namespace (and not attached):
[1] tools_2.10.1

View this message in context: http://n4.nabble.com/Convert-data-frame-to-XML-Tree-tp1478281p1478356.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list