Created
February 7, 2011 16:04
-
-
Save miceno/814594 to your computer and use it in GitHub Desktop.
Groovy XML Builder to DOM and Document
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.restlet.data.MediaType; | |
| import org.restlet.resource.DomRepresentation; | |
| import org.w3c.dom.Document | |
| // Representation | |
| DomRepresentation representation = new DomRepresentation( MediaType.TEXT_XML) | |
| // Get the document root | |
| Document doc= representation.getDocument() | |
| // Get a builder for the document | |
| def builder=new groovy.xml.DOMBuilder( doc) | |
| // Build the DOM with the Builder | |
| def dom= builder.html{ | |
| body{ | |
| 10.times{ | |
| h1 "titulo $it" | |
| p "atendiendo a mi parrafo numero $it" | |
| } | |
| } | |
| } | |
| // Add the built DOM to the Document | |
| doc.appendChild( dom) | |
| use( groovy.xml.dom.DOMCategory){ | |
| println dom.'**'.h1.text() | |
| } | |
| /* | |
| titulo 0titulo 1titulo 2titulo 3titulo 4titulo 5titulo 6titulo 7titulo 8titulo 9 | |
| */ | |
| // Write the representation (DOM) to standard output | |
| representation.write( System.out) | |
| /* | |
| <?xml version="1.0" encoding="UTF-8"?> | |
| <html> | |
| <body> | |
| <h1>titulo 0</h1> | |
| <p>atendiendo a mi parrafo numero 0</p> | |
| <h1>titulo 1</h1> | |
| <p>atendiendo a mi parrafo numero 1</p> | |
| <h1>titulo 2</h1> | |
| <p>atendiendo a mi parrafo numero 2</p> | |
| <h1>titulo 3</h1> | |
| <p>atendiendo a mi parrafo numero 3</p> | |
| <h1>titulo 4</h1> | |
| <p>atendiendo a mi parrafo numero 4</p> | |
| <h1>titulo 5</h1> | |
| <p>atendiendo a mi parrafo numero 5</p> | |
| <h1>titulo 6</h1> | |
| <p>atendiendo a mi parrafo numero 6</p> | |
| <h1>titulo 7</h1> | |
| <p>atendiendo a mi parrafo numero 7</p> | |
| <h1>titulo 8</h1> | |
| <p>atendiendo a mi parrafo numero 8</p> | |
| <h1>titulo 9</h1> | |
| <p>atendiendo a mi parrafo numero 9</p> | |
| </body> | |
| </html> | |
| */ | |
| // Print the DOM to standard output | |
| println dom | |
| /* | |
| <?xml version="1.0" encoding="UTF-8" standalone="no"?><html><body><h1>titulo 0</h1><p>atendiendo a mi parrafo numero 0</p><h1>titulo 1</h1><p>atendiendo a mi parrafo numero 1</p><h1>titulo 2</h1><p>atendiendo a mi parrafo numero 2</p><h1>titulo 3</h1><p>atendiendo a mi parrafo numero 3</p><h1>titulo 4</h1><p>atendiendo a mi parrafo numero 4</p><h1>titulo 5</h1><p>atendiendo a mi parrafo numero 5</p><h1>titulo 6</h1><p>atendiendo a mi parrafo numero 6</p><h1>titulo 7</h1><p>atendiendo a mi parrafo numero 7</p><h1>titulo 8</h1><p>atendiendo a mi parrafo numero 8</p><h1>titulo 9</h1><p>atendiendo a mi parrafo numero 9</p></body></html> | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment