[R] example - export a data frame to an XML file

Paul Murrell p.murrell at auckland.ac.nz
Tue Sep 30 03:33:57 CEST 2008


Hi

Is this what you are after ... ?


data <-
read.csv(textConnection('"date","UYG.Open","UYG.High","UYG.Low","UYG.Close","UYG.Volume","UYG.Adjusted"
"2007-02-01",71.32,71.34,71.32,71.34,200,69.23
"2007-02-02",72.2,72.2,72.2,72.2,200,70.06
"2007-02-05",71.76,71.76,71.76,71.76,5100,69.63
"2007-02-06",72.85,72.85,72.85,72.85,3800,70.69
"2007-02-07",72.85,72.85,72.85,72.85,0,70.69'),
                  as.is=TRUE)

library(XML)

xml <- xmlTree()
xml$addTag("document", close=FALSE)
for (i in 1:nrow(data)) {
    xml$addTag("row", close=FALSE)
    for (j in names(data)) {
        xml$addTag(j, data[i, j])
    }
    xml$closeTag()
}
xml$closeTag()

# view the result
cat(saveXML(xml))


Paul


zubin wrote:
> Duncan, thanks for the note - the schema looks like this - is this what 
> your asking for?
> 
> <document>
>  <row>
>   <date>2007-02-01</date >
>   <UYG_Open>71.32</UYG_Open >
>   <UYG_High>71.34</UYG_High >
>   <UYG_Low>71.32</UYG_Low >
>   <UYG_Close>71.34</UYG_Close >
>   <UYG_Volume>200</UYG_Volume >
>   <UYG_Adjusted>69.23</UYG_Adjusted >
>  </row>
>  <row>
>   <date>2007-02-02</date >
>   <UYG_Open>72.2</UYG_Open >
>   <UYG_High>72.2</UYG_High >
>   <UYG_Low>72.2</UYG_Low >
>   <UYG_Close>72.2</UYG_Close >
>   <UYG_Volume>200</UYG_Volume >
>   <UYG_Adjusted>70.06</UYG_Adjusted >
>  </row>
> </document>
> 
> Duncan Temple Lang wrote:
>> Hi Zubin.
>>
>>
>>   The first thing is to send us a link to the schema that the
>> other application expects. That would give us a "one-to-one"
>> mapping; otherwise, data.frame to arbitrary XML is to vague.
>>
>>   Currently, there is nothing in the XML package that would
>> be able to take an XML schema and write an R object to such a
>> structure. However, I am in the process of creating an XMLSchema
>> package which can read XML schema and make the description available to
>> R so that we can read data from an XML file corresponding to that schema
>> and this could be adapted in some ways to automate the creation of
>> XML from an R object (under certain assumptions).
>>
>>  So please send us all a link to the XML schema.
>>
>>   D.
>>
>>
>> zubin wrote:
>>> In need of exporting an XML file from R, I scrub some data in R and 
>>> push the data into another application requiring XML.  The data set 
>>> is a very straightforward data frame of stock prices- see below.  I 
>>> know the package XML is the one to use, but need an example or some 
>>> direction on where to start.  I know we need to define the schema - 
>>> can this be done in the R XML package?
>>>> "date","UYG.Open","UYG.High","UYG.Low","UYG.Close","UYG.Volume","UYG.Adjusted" 
>>>>
>>>> "2007-02-01",71.32,71.34,71.32,71.34,200,69.23
>>>> "2007-02-02",72.2,72.2,72.2,72.2,200,70.06
>>>> "2007-02-05",71.76,71.76,71.76,71.76,5100,69.63
>>>> "2007-02-06",72.85,72.85,72.85,72.85,3800,70.69
>>>> "2007-02-07",72.85,72.85,72.85,72.85,0,70.69
>>>>
>>>>
>>>>
>>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide 
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list