File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Groovy and the fly likes How to write XML to a new file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "How to write XML to a new file." Watch "How to write XML to a new file." New topic

How to write XML to a new file.

Brian Oleksa

Joined: Jun 05, 2007
Posts: 15
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

Joined: May 14, 2008
Posts: 5852

There has to be an easier way (but I can't find the example), but this should work:

JBoss In Action
Marc Peabody
pie sneak

Joined: Feb 05, 2003
Posts: 4727

Your file object has both write() and append() methods that accept a string. write() replaces the file contents while append() just adds to it.

A good workman is known by his tools.
I agree. Here's the link:
subject: How to write XML to a new file.
It's not a secret anymore!