Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Many XML parsers have switches to turn on DTD validation.

Suppose we have the following XML:

Code Block
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+)>
	<car name="HSV Maloo" make="Holden" year="2006">
		<record type="speed">Production Pickup Truck with speed of 271kph</record>
	<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 name="Royale" make="Bugatti" year="1931">
		<record type="price">Most Valuable Car at $15 million</record>

We can parse this document with validation turned on using the XmlParser as follows:

Code Block
def validating = true        // default is false
def namespaceAware = false   // default is true
new XmlParser(validating, namespaceAware).parseText(xml)

Or using the XmlSlurper as follows:

Code Block
new XmlSlurper(validating, namespaceAware).parseText(xml)

Or using the DOMBuilder as follows:

Code Block
groovy.xml.DOMBuilder.parse(new StringReader(xml), validating, namespaceAware)