Using DTDs in Java

One method of constraining and validating the content of XML files is the Document Type Definition. Although part of the XML specification, DTDs (and XML files that reference them) aren't quite as straight-forward to parse as the standalone </>-type syntax we've come to know and love.

But when one does need to parse them, there are tools in various languages, including Java.

Parsing DTDs with Stylus Studio®

Within Stylus Studio®, parsing DTDs with Java takes place when you use the Xerces-J validator in the XML editor.

You can also add, as a Custom Validation Engine, the Sun Multi Schema Validator which will parse DTDs as part of its validation steps.

Parse DTD in Java

It can also be invoked implicitly as part of the XML parsing process when running the various Java-based XSLT engines, such as Saxon and Xalan-J.

Parsing DTDs in your own Java Code

The two main methods of parsing DTDs are DOM (Document Object Model) and SAX (Simple API for XML). The difference is that the DOM builds the entire representation of the document in memory before parsing begins. But with SAX, as each item is seen as the document is parsed, an event is 'fired' into your program, and it is up to you to keep track of what you care about.

The DOM interface in Java is well documented and managed by the World-Wide Web Consortium (W3C).

The SAX interface is modular, and in SAX 2 includes several classes specifically designed for tracking SAX events. These include the following interfaces and methods, according to the SAX Project API documents. (This is not an exhaustive list, but an overview of some of the more useful and DTD-specific methods.)

  • org.xml.sax.DTDHandler
    • void notationDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
    • void unparsedEntityDecl(java.lang.String name, java.lang.String publicId, java.lang.String systemId, java.lang.String notationName)
  • org.xml.sax.ext.Attributes2
    • boolean isDeclared(int index)
    • boolean isDeclared(java.lang.String qName)
    • boolean isDeclared(java.lang.String uri, java.lang.String localName)
    • boolean isSpecified(int index)
    • boolean isSpecified(java.lang.String qName)
    • boolean isSpecified(java.lang.String uri, java.lang.String localName)
  • org.xml.sax.ext.LexicalHandler
    • void startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
    • void endDTD()

Edit and Debug Java Source

Now, to write the code, you need an editor. Stylus Studio® includes a built-in Java Editor and Debugger and powerful Java code generation facilities. Take it out for a free test drive, and parse those DTDs!.


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Try Stylus DTD Generator

Generate DTDs with our award-winning DTD conversion and DTD generation utilities - Download a free trial!

What's New for Stylus Studio® X16?

New XQuery & Web Services Tools, Support for MySQL, PostgreSQL, HL7 EDI, Microsoft .NET Code Generation and much more!

Why Pay More for XML Tools?

With Stylus Studio® X16 XML Enterprise Suite, you get the most comprehensive XML tool suite at one incredibly low price. Value: it's just one of many reasons why smart XML developers are choosing Stylus Studio!

Free Stylus Studio XML Training:
W3C Member