dtddoc step 1: Parsing a DTD (2/2) - exploring XML | WebReference

dtddoc step 1: Parsing a DTD (2/2) - exploring XML

dtddoc step 1: Parsing a DTD

Wutka's DTD parser

Wutka's parser works the same way, but using a java.io.Reader in the constructor instead of the extra SAX inputsource parameter:

import java.io.FileReader;
import com.wutka.dtd.DTDParser;
import com.wutka.dtd.DTD;
DTDParser parser = new DTDParser(new FileReader("html4.dtd"));
DTD dtd = parser.parse();

We will examine the different DTD objects in the next article of this series.

perlSGML's DTDParser

The perlSGML library has been around in a stable version for many years, the latest dated September 18, 1997. It has many tools for manipulating SGML documents, and by logical extension XML and DTD formats. perlSGML also includes the previously introduced dtd2html for documenting DTDs.

Traditionally, Perl programs make less use of classes, in favor of a more functional programming style around the built-in hash and array data types. This put aside, reading and parsing a DTD works similar to the Java examples above:

require "dtd.pl";
$FILEHANDLE = open("html4.dtd");
$status = &DTDread_dtd(FILEHANDLE);

If DTDread_dtd() successfully parsed a DTD, it returns 1, otherwise 0. The resulting parsed data structure is held in package scope variables, creating problems for concurrency and thread safety if not handled with care. In the context of handling a single CGI request these problems are not an issue, though.

DTD Parser in phpxmlclasses

PHP can be used in both Java and Perl style in terms of programming. The PHP XML classes, as the name implies, follows a more object-oriented rather than functional paradigm:

include_once("DTD_parser.html");
$r = new URLReader("html4.dtd");
$parser = new DTDparser($r,false);		
$dtd = $parser->parse(true);  

Conclusion

Although code for dealing with DTDs exists in many programming languages, few applications are available, short of SGML/XML editors with built-in DTD support. In the coming weeks we will make use of this code to create a more powerful DTD documentation tool than dtd2html, along the lines of javadoc.


Produced by Michael Claßen

URL: http://www.webreference.com/xml/column65/2.html
Created: Sep 30, 2002
Revised: Sep 30, 2002