Skip to content

XML to data frame or list

3 messages · bogdan romocea, Gabor Grothendieck, Barry Rowlingson

#
Dear useRs,

I have a simple/RTFM question about XML parsing. Given an XML file,
such as (fragment)
<A>100</A>
<B>23</B>
<C>true</C>
how do I import it in a data frame or list, so that the values (100,
23, true) can be accessed through the names A, B and C?

I installed the XML package and looked over the documentation...
however after 20 minutes and a couple of tests I still don't know what
I should start with. 

Can someone provide an example or point me to the appropriate
function(s)?

Thank you,
b.
#
bogdan romocea <br44114 <at> yahoo.com> writes:

: 
: Dear useRs,
: 
: I have a simple/RTFM question about XML parsing. Given an XML file,
: such as (fragment)
: <A>100</A>
: <B>23</B>
: <C>true</C>
: how do I import it in a data frame or list, so that the values (100,
: 23, true) can be accessed through the names A, B and C?
: 
: I installed the XML package and looked over the documentation...
: however after 20 minutes and a couple of tests I still don't know what
: I should start with. 
: 
: Can someone provide an example or point me to the appropriate
: function(s)?
: 

You could check out the ctv package that was recently announced.
It uses XML so its source would provide an example.

If its a one-time operation, Excel reads XML and you could then
use one of the many Excel to R possibilities.
#
Gabor Grothendieck wrote:

            
For an xml file like this:

<?xml version="1.0"?>
<variables>
<a>100</a>
<b>23</b>
<z>666</z>
</variables>

its a one-liner with the XML package (library(XML)):

xmlReadSimple <-
function(xmlFile){
   as.list(xmlSApply(xmlRoot(xmlTreeParse(xmlFile)),xmlValue))
}

add an lapply(...,as.numeric) for conversion to numbers.

  sweet.

Baz