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 :
156
The org.xml.sax.ext.LexicalHandler Handler
All Known Subinterfaces: java.xml.transform.sax.TransformerHandler
This interface defines callback
methods that can receive notification of several lexical events such as
comments, entity declarations, DTD declarations, and CDATA sections. This does
not affect the resulting data within the XML document. In ContentHandler, these lexical events are essentially ignored, and
you just get the data and declarations without notification of when or how they
were provided.
This is not really a general-use
handler, as most applications don’t need to know if text was in a CDATA section
or not. However, if you are working with an XML editor, serializor, or just its
contents, the LexicalHandler can really help you out.
Method Overviews
As I have already mentioned in the overview, methods in this interface
receives notification for lexical events. We generally don’t use this interface
in our parsers but for advance use, I have given examples of methods which I
generally use in my application.
Example of comment()
Report an XML comment anywhere in the document. This callback will be
used for comments inside or outside the document element, including comments in
the external DTD subset (if read). Comments in the DTD must be properly nested
inside start/endDTD and start/endEntity events (if used).
/**
* Report an XML comment anywhere in the
document.
* @param ch An array holding the characters
in the comment.
* @param start The starting position in the
array.
* @param length The number of characters to
use from the array.
*/
public void comment(char[]
ch, int start, int length)
* This
method reports an element type declaration(<!ELEMENT) in DTD.
* @param
name The element type name.
* @param
model The content model as a normalized string.
*/
public void elementDecl(String name, String model)
{
System.out.println("Start of elementDecl");
System.out.println("name :"+name);
System.out.println("model :"+model);
System.out.println("End of elementDecl");
}
Example of startDTD()
Report
the start of DTD declarations, if any. This method is intended to report the
beginning of the DOCTYPE declaration; if the document has no DOCTYPE
declaration, this method will not be invoked.
/**
* Report
the start of DTD declarations, if any.
* @param
name The document type name.
* @param
publicId The declared public identifier for the external DTD subset, or null if
none was declared.
* @param
systemId The declared system identifier for the external DTD subset, or null if
none was declared.
*/
public void startDTD(String name, String publicId, String systemId)
{
System.out.println("startDTD");
System.out.println("name :"+name);
System.out.println("public Id :"+publicId);
System.out.println("System Id :"+systemId);
}
Example of How to use LexicalHandler
LexicalHandlerImpl lexicalHandler = new
LexicalHandlerImpl();//This class implements LexicalHandler
/*
To set
the LexicalHandler for an XML reader, use the setProperty
method with the propertyId
"http://xml.org/sax/properties/lexical-handler". If the reader does
not support lexical events, it will throw a SAXNotRecognizedException or a SAXNotSupportedException when you attempt
to register the handler.