Java Reference
In-Depth Information
The SongXMLConverter class has four methods: one to convert a single song to XML,
one to convert XML to a single song, and two to do the same for a collection of songs.
Converting from XML to Song instances is done with the XmlSlurper illustrated earli-
er. The only new part is that the slurper accesses the song ID value using the @id notation,
where the @ is used to retrieve an attribute. Figure 4.7 shows the job of the XmlSlurper ,
or its analogous class, XmlParser .
Figure 4.7. Using an XmlSlurper or XmlParser to populate an object from XML data
Going the other direction, from song to XML, is done with a MarkupBuilder . The
MarkupBuilder class writes to standard output by default. In this class I want to return
the XML as a string, so I used the overloaded MarkupBuilder constructor that takes a
java.io.Writer as an argument. I supply a StringWriter to the constructor, build
the XML, and then convert the output to a String using the normal toString method.
 
Search WWH ::




Custom Search