Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write XML to a new file.

 
Brian Oleksa
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can easily generate XML with groovy and output it to the groovy console.

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.gpx() { wpt(lat:’22.22222’, lon:’77.77777’) }
println writer.toString()

I also know how to create a new file.

def target ="C:/gps.xml"
File file= new File(target)

But how do I write the XML to a new file called gps.xml..?

Thanks in advanced.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There has to be an easier way (but I can't find the example), but this should work:

 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your file object has both write() and append() methods that accept a string. write() replaces the file contents while append() just adds to it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic