user1780424
user1780424

Reputation: 413

how to output a vector to xml in r

I have a vector of integer values. for example

vec = c(2,52,12,56,121)

I want to write an XML file which outputs these values. For example, the XML file may look like this:

<Periods>
  <Value>2</Value>
  <Value>52</Value>
  <Value>12</Value>
  <Value>56</Value>
  <Value>121</Value>
</Periods>

What is the most efficient way in R to use the "XML" package to achieve this?

Thanks.

Upvotes: 1

Views: 341

Answers (1)

adaien
adaien

Reputation: 1942

library(XML)
node = newXMLNode("Periods")
sapply(as.character(vec),function(x){
    newXMLNode("Value",x,parent=node)
})
saveXML(node,file="try.xml")

Upvotes: 1

Related Questions