Skip to content
Prev 316033 / 398513 Next

Create a Data Frame from an XML

Hello Adam,
I had a similar problem with a big dataframe, and building an xmlTree in
the clean way was extremely slow; so i resorted to manual method. Not
tested, but if your dataframe is my_df, then something like the
following should do:

buildEntry <- function(x) {
    cat(paste('<z:row BRAND="', x[1],
                     '" NUM="', x[2],
                     '" YEAR="', x[3],
                     '" VALUE="', x[4],
                     '"/>\n', sep=""))
    }

sink(paste('my_file.xml', sep=""))

cat ('<?xml version="1.0" encoding="ISO-8859-1"?>\n')
cat ('<data xmlns="uri://localhost/z">\n')
 
# invisible avoids returning a NULL in the file
invisible(apply(my_df, 1, buildEntry))
cat ("</data>" )
sink()

And it took very little time.

HTH,
Gabriele


-----Original Message-----
From: Adam Gabbert [mailto:adamjgabbert at gmail.com] 
Sent: Wednesday, January 23, 2013 5:36 PM
To: Duncan Temple Lang; btupper at bigelow.org
Cc: r-help at r-project.org
Subject: Re: [R] Create a Data Frame from an XML

Hello Gentlemen,

I mistakenly sent the message twice, because the first time I didn't
receive a notification message so I was unsure if it went through
properly.

Your solutions worked great. Thank you!  I felt like I was fairly close
just couldn't quite get the final step.

Now, I'm trying to reverse the process and account for my header.

In other words I have my data frame in R:

BRAND    NUM    YEAR    VALUE
GMC        1          1999      10000
FORD       2          2000      12000
GMC        1          2001       12500
     etc........
and I make some edits.
BRAND    NUM    YEAR    VALUE
DODGE       3          1999      10000
TOYOTA       4         2000      12000
DODGE        3          2001       12500
So now I would need to ouput an XML file in the same format accounting
for my header (essentially, add "z:" in front of row).

(What I want to output)
Thus far from the help I've found online I was trying to set up an
xmlTree xml <- xmlTree()

and use xml$addTag to create nodes and put in the data from my data
frame.
I feel like I'm not really even close to a solution so I'm starting to
believe that this might not be the best path to go down.

Once again, any help is much appreciated.

AG


On Tue, Jan 22, 2013 at 6:04 PM, Duncan Temple Lang
<dtemplelang at ucdavis.edu