Many XML parsers have switches to turn on DTD validation.
Suppose we have the following XML:
def xml = '''
<!DOCTYPE records [
<!ELEMENT car (country,record)>
<!ATTLIST car
make NMTOKEN #REQUIRED
name CDATA #REQUIRED
year NMTOKEN #REQUIRED
>
<!ELEMENT country (#PCDATA)>
<!ELEMENT record (#PCDATA)>
<!ATTLIST record type NMTOKEN #REQUIRED>
<!ELEMENT records (car+)>
]>
<records>
<car name="HSV Maloo" make="Holden" year="2006">
<country>Australia</country>
<record type="speed">Production Pickup Truck with speed of 271kph</record>
</car>
<car name="P50" make="Peel" year="1962">
<country>Isle of Man</country>
<record type="size">Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
</car>
<car name="Royale" make="Bugatti" year="1931">
<country>France</country>
<record type="price">Most Valuable Car at $15 million</record>
</car>
</records>
'''.trim()
|
We can parse this document with validation turned on using the XmlParser as follows:
def validating = true // default is false def namespaceAware = false // default is true new XmlParser(validating, namespaceAware).parseText(xml) |
Or using the XmlSlurper as follows:
new XmlSlurper(validating, namespaceAware).parseText(xml) |
Or using the DOMBuilder as follows:
groovy.xml.DOMBuilder.parse(new StringReader(xml), validating, namespaceAware) |