Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
178
Overview of DTD Handlers
There are four
core handler interfaces defined by SAX 2.0: org.xml.sax.ContentHandler,
org.xml.sax.ErrorHandler, org.xml.sax.DTDHandler, and
org.xml.sax.EntityResolver. In this scenario we discuss DTDHandler. This
interface defines methods you will generally never be called upon to use. Used
when processing a DTD to recognize and act on declarations for an unparsed
entity.
The org.xml.sax.DTDHandler Handler
All Known Subinterfaces: javax.xml.transform.sax.TransformerHandler
All Known Implementing Classes: org.xml.sax.helpers.DefaultHandler,
org.xml.sax.HandlerBase, org.xml.sax.helpers.XMLFilterImpl
The DTDHandler interface allows you to receive notification when a reader encounters an unparsed entity or a
notation declaration. Remember that both of these events occurs in DTDs, not
XML Documents, which is why this is called DTDHandler. You can associate your
DTDHandler implementation class by using set setDTDHandler() method of
XMLReader.
Method Overviews
You generally create an implementation of DTDHandler and register it with
your reader through the XMLReader’s setDTDHandler() method. This is generally
useful when writing low-level applications that must either reproduce XML
content( such as an XML Editor), or when you want to build up some Java
representation of a DTD’s constraints( such as for data bindings). But in most
of the cases, it is not something you will need very soon.
Example of a DTD
<?xml version="1.0" encoding="UTF-8"?>
<!NOTATION person SYSTEM
"databas/employees">
<!ENTITY topxml_logo SYSTEM "http://www.topxml.com/jpeg/topxml.jpeg"
NDATA jpeg>
<!ELEMENT Person (First, Last, Title, Phone, Email)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Client-name (#PCDATA)>
<!ELEMENT First (#PCDATA)>
<!ELEMENT Last (#PCDATA)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Phone (#PCDATA)>
<!ELEMENT Email (#PCDATA)>
Example of notationDecl()
public void notationDecl(String name, String
publicId, String systemId)
{
System.out.println("Name:"+name+", Public
Id:"+publicId+", SystemId :"+systemId);
}
On execution of this method you will get the output:
Name:person, Public Id:null,
SystemId :databas/employees
Example of unparsedEntityDecl()
public void unparsedEntityDecl(String name,
String publicId, String systemId, String notationName)
{
System.out.println("Name:"+name+", Public
Id:"+publicId+", SystemId"+systemId+", Notation
Name:"+notationName);
}
On execution of above method you
will get the output:
Name:topxml_logo, Public Id:null,
SystemIdhttp://www.topxml.com/jpeg/topxml.jpeg, Notation Name:jpeg
Example of how to use DTDHandler
DTDHandlerImpl dtdHandler = new
DTDHandlerImpl();//This class implements DTDHandler interface
xmlReader.setDTDHandler(dtdHandler);//association
of DTDHandlerImpl with XMLReader