DATA MINING
Desktop Survival Guide by Graham Williams |
|||||
XML for Data |
Example from Paul Murrell, 30 Sep 2008 on r-help:
> ds <- read.csv(textConnection( '"date","Open","High","Low","Close","Volume","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(ds)) { xml$addTag("row", close=FALSE) for (j in names(ds)) { xml$addTag(j, ds[i, j]) } xml$closeTag() } > xml$closeTag() # view the result > cat(saveXML(xml)) |
The result is:
<?xml version="1.0"?> <document> <row> <date>2007-02-01</date> <Open>71.32</Open> <High>71.34</High> <Low>71.32</Low> <Close>71.34</Close> <Volume>200</Volume> <Adjusted>69.23</Adjusted> </row> [...] <row> <date>2007-02-07</date> <Open>72.85</Open> <High>72.85</High> <Low>72.85</Low> <Close>72.85</Close> <Volume>0</Volume> <Adjusted>70.69</Adjusted> </row> </document> |