Convert XML to dataframe, edit, and then convert back to XML in R?

I have an XML document that has a fairly ordered structure (although I have nothing good, like a schema or DTD). I want to change / add / delete some of the elements of my XML file using others.

To do this, I want to take my XML file, convert it to a data framework with one row for each tag and one column per attribute, and then edit the data frame (or export to csv, change someone else, and then back off) and then convert back to XML.

Currently, this code performs the first part of converting XML to dataframe

foo<-xmlParseDoc("example.xml") bar<-xmlSApply(xmlRoot(foo), xmlAttrs, addNamespaceURLs=TRUE) tempdf<-data.frame() templist<-list() n<-1 for(i in seq(length(bar))){ message(i) if(!is.null(bar[[i]])){ message(bar[[i]]) tempdf<-rbind.fill(tempdf, data.frame(t(bar[[i]]), stringsAsFactors=FALSE)) n<-n+1 } } 

but I was fixated on how to convert this framework back to the XML equivalent. Any ideas?

+4
source share
1 answer

What about the xmlToDataFrame package? I would suggest that it has methods not only for reading xml for a frame, but also for writing a frame to an XML file. Never used it myself though

-2
source

Source: https://habr.com/ru/post/1389495/


All Articles