Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating XML file in R and saving

Tags:

r

xml

I have a problem with saving XML file from R.

Firstly I write my code here:

doc = newXMLDoc()

document = newXMLNode("Document", doc = doc)
set = newXMLNode("Settings", parent = document)
  elements = newXMLNode("Elements", parent = set)
    newXMLNode("Canvas", parent = elements, attrs = c(Id = "1"))
    newXMLNode("Canvas", parent = elements, attrs = c(Id = "2"))

objcol = newXMLNode("ObjectCollection", parent = document)
  timeSeries1 = newXMLNode("Timeseries", parent = objcol)
  timeSeries2 = newXMLNode("Timeseries", parent = objcol)

saveXML(doc, file="test.dtv", indent = T, 
      prefix = '<?xml version="1.0" encoding="utf-8" standalone="no"?>\n')

So, if I save doc without prefix, all is good, but i haven't prefix in my ouput file. When I add prefix attribute to function saveXML, output is really bad. It has only one '\n' after prefix(because I write it in prefix string), but all document is on one line. I haven't ideas how to fix it. Thank you for your attention.

like image 468
Aslan Bayramkulov Avatar asked Oct 16 '25 17:10

Aslan Bayramkulov


1 Answers

So, I'am also quite surprised why this is not working, but found a "workaround" to it. Hope this is helpfull.

 cat(saveXML(doc, 
             indent = TRUE, 
             prefix = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n"),
     file="test.dtv")
like image 88
ricoderks Avatar answered Oct 18 '25 10:10

ricoderks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!