Java Reference
In-Depth Information
mentation ultimately is to generate an XML element with the method name as the element
name.
The braces that follow are interpreted to mean a child element is next. The name of the
child element will be deptName , and its character data will be the supplied string. The
next element is an employee , and the map-like syntax for the id implies an attribute on
the employee element is needed, and so on.
The result of executing this script is
<department>
<deptName>Construction</deptName>
<employee id='1'>
<empName>Fred</empName>
</employee>
<employee id='2'>
<empName>Barney</empName>
</employee>
</department>
The MarkupBuilder generates the XML. It's hard to imagine a simpler way to solve
that problem.
I want to illustrate one final aspect of XML processing with Groovy, which involves valid-
ating a document.
B.8.3. Validation
XML documents are validated in one of two ways: through either a Document Type Defin-
ition (DTD) or an XML schema. The DTD system is older, simpler, and much less use-
ful, but the Java parsers have been able to validate against them almost from the begin-
ning. Schema validation came much later but is far more important, especially when deal-
ing with, for example, web services.
Validating XML with Groovy is an interesting demonstration both of what Groovy
provides, and what to do if Groovy doesn't provide anything.
First, consider validation against a DTD. Here's a DTD for the library XML shown earlier:
Search WWH ::




Custom Search