Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # load package
- library(XML)
- data("ChickWeight")
- mydata<- cbind(ID=seq(1, nrow(ChickWeight),by=1),ChickWeight)
- start.time <- Sys.time()
- xml <- xmlTree()
- xml$addTag("document", close=FALSE)
- for (i in 1:nrow(mydata)) {
- xml$addNode("chicken", attrs = c(ID = mydata[i,"ID"]), close = FALSE)
- out<- names(mydata)[names(mydata) != "ID"]
- for (j in out) {
- xml$addNode(j, df[i, j])
- }
- xml$closeTag()
- }
- xml$closeTag()
- # Thats how the XML shoul look like
- cat(saveXML(xml))
- end.time <- Sys.time()
- time.taken <- end.time - start.time
- time.taken
- #Time difference of 5.847585 secs
- # save as matrix
- mat<- as.matrix(cbind(ID=seq(1, nrow(ChickWeight),by=1),ChickWeight))
- start.time <- Sys.time()
- xml <- xmlTree()
- xml$addTag("document", close=FALSE)
- for (i in 1:dim(mat)[1]) {
- xml$addNode("chicken", attrs = c(ID = mat[i,1]), close = FALSE)
- out<- names(mat)[names(mat) != "ID"]
- for (j in out) {
- xml$addNode(j, mat[i, j])
- }
- xml$closeTag()
- }
- xml$closeTag()
- # Which gives wrong format
- cat(saveXML(xml))
- end.time <- Sys.time()
- time.taken <- end.time - start.time
- time.taken
- #Time difference of 1.853185 secs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement