Java Reference
In-Depth Information
Several times I've written applications that took this script, after converting it to a class
that used a
Location
like before, and added it as a service. The code savings over the
corresponding Java version is just too great to ignore.
Parsing is one thing, but what about generation? For that, Groovy provides a builder class
called
groovy.xml.MarkupBuilder
.
Consider another POJO representing a
Song
, as shown here:
public class Song {
private int id;
private String title;
private String artist;
private String year;
public Song() {}
public Song(int id, String title, String artist, String year) {
this.id = id;
this.title = title;
this.artist = artist;
this.year = year;
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public String getArtist() { return artist; }
public void setArtist(String artist) { this.artist = artist; }
public String getYear() { return year; }
public void setYear(String year) { this.year = year; }
}
The
Song
class,implementedinJava,containsan
id
andstringsforthe
title
,
artist
,
and
year
.The rest isjust constructors, getters, andsetters. Inareal system the class would
also probably have overrides of
toString
,
equals
, and
hashCode
, but I don't need
that here.
How should
Song
instances be represented in XML? One simple idea would be to treat the
ID as an attribute of the song, and have
title
,
artist
, and
year
as child elements. In