RESTful初探之五(Generating XML documents)
In the Formatting the resource,wo decided to leverage XML as the data mechanism for sharing information between clients and the service.your Restlets,therefore,must manipulate XML:build it in the case of a GET and consume it in the case of a POST,PUT,or DELETE.In this section,you'll take the pain out of generating and manipulating XML documents by leveraging the Groovy scriping language.
之前格式化resource中讲的,我们决定使用XML作为数据机制。但是XML的生成与操作非常痛苦,幸运的是,我们可以使用Groovy来简化。
Leveraging Groovy
Working with XML is no easy chore.It can be tedious and error prone,to say the least.Fortunately,Groovy makes working with XML much easier.
You'll leverage Groovy's power to generate XML and do the tedious job of manipulating XML documents.Working with XML in Groovy couldn't be any easier.For instance,parsing an XML document is a cakewalk.Take the XML document in Listing7:
你将使用Groovy来生产和操作XML。
<acme-races> <race name="Alaska 200 below" date="Thu Jan 01" distance="3.2" id="20"> <uri>/races/20</uri> <description>Enjoy the cold!</description> </race></acme-races>
def root = new XmlSlurper().parseText(raceXML)def name = root.race.@name.text()
def writer = new StringWriter()def builder = new MarkupBuilder(writer)builder."acme-races"() { race(name: "Alaska 200 below", date: "Thu Jan 01", distance: "3.2", id: "20") { uri("/races/20") description("Enjoy the cold!") }}println writer.toString()