File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
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
Author

How to write XML to a new file.

Brian Oleksa
Greenhorn

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
author
Bartender

Joined: May 14, 2008
Posts: 5845
    
    7

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
Sheriff

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: http://aspose.com/file-tools
 
subject: How to write XML to a new file.