SophiaFramework UNIVERSE 5.3 |
#include <SFXXMLDefaultHandler.hpp>
class SFXXMLDefaultHandler;
SFMTYPEDEFCLASS(SFXXMLDefaultHandler)
In the SAX parser(SFXXMLSAXParser), an XML document is sequentially read from the beginning, and an event is generated every time the start or the end of an element, or the CDATA section is detected. The event is notified to an appropriate handler function of the class that inherits from the SFXXMLDefaultHandler class and is processed.
The event order depends on the content of the XML document. For example, the content of a certain element (such as charater data, processing instruction, sub-element) is sequentially lined up between the StartElement event and the EndElement event.
To use the SAX parser(SFXXMLSAXParser), implement the handler class that inherits from the SFXXMLDefaultHandler class, and register the instance of handler class to the SAX parser(SFXXMLSAXParser) using the SFXXMLSAXParser::SetDefaultHandler function.
The SAX parser(SFXXMLSAXParser) is faster and takes much less memory than the DOM parser(SFXXMLDOMParser).
Example 850. The method to define and implement the handler class
// handler class class MyXMLHandler : public SFXXMLDefaultHandler { public: explicit MyXMLHandler(Void); virtual ~MyXMLHandler(Void); // declaration for each handler // handlers for obtaining content of XML document virtual Void Characters(SFXAnsiStringConstRef string, BoolConst cdataSection = true); virtual Void EndDocument(Void); virtual Void EndElement(SFXAnsiStringConstRef uri = SFXAnsiString::EmptyInstance(), SFXAnsiStringConstRef localname = SFXAnsiString::EmptyInstance(), SFXAnsiStringConstRef qname = SFXAnsiString::EmptyInstance()); virtual Void StartDocument(Void); virtual Void StartElement(SFXAnsiStringConstRef uri, SFXAnsiStringConstRef localname, SFXAnsiStringConstRef qname, SFXXMLGrammar::XMLAttrListConstRef attrList); virtual Void IgnorableWhitespace(SFXAnsiStringConstRef string); virtual Void ProcessingInstruction(SFXAnsiStringConstRef target, SFXAnsiStringConstRef data); virtual Void EndPrefixMapping(SFXAnsiStringConstRef prefix); virtual Void StartPrefixMapping(SFXAnsiStringConstRef prefix, SFXAnsiStringConstRef uri); // handlers for parsing XML document virtual Void Comment(SFXAnsiStringConstRef string); virtual Void EndCDATA(Void); virtual Void EndDTD(Void); virtual Void EndEntity(SFXAnsiStringConstRef name); virtual Void StartCDATA(Void); virtual Void StartDTD(SFXAnsiStringConstRef name, SFXAnsiStringConstRef publicId, SFXAnsiStringConstRef systemId); virtual Void StartEntity(SFXAnsiStringConstRef name); // handlers for processing DTD virtual Void NotationDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef publicId, SFXAnsiStringConstRef systemId); virtual Void UnparsedEntityDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef publicId, SFXAnsiStringConstRef systemId, SFXAnsiStringConstRef notationName); // handlers to be notified of having received each declaration virtual Void ElementDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef model); virtual Void AttributeDecl(SFXAnsiStringConstRef ename, SFXAnsiStringConstRef aname, SFXAnsiStringConstRef type, SFXAnsiStringConstRef valuedefault, SFXAnsiStringConstRef value); virtual Void InternalEntityDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef value); virtual Void ExternalEntityDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef publicId, SFXAnsiStringConstRef systemId); // handlers to be notified of having received error message virtual Void ErrorReport(SFCErrorConst error, SFXAnsiStringConstRef errInfo); }; // constructor MyXMLHandler::MyXMLHandler(Void) { return; } // destructor MyXMLHandler::~MyXMLHandler(Void) { return; } // handler to receive the notification of the start of a document Void MyXMLHandler::StartDocument(Void) { TRACE("document start"); } // handler to receive the notification of the end of a document Void MyXMLHandler::EndDocument(Void) { TRACE("document end"); } // handler to receive the notification of the start of an element Void MyXMLHandler::StartElement(SFXAnsiStringConstRef uri, SFXAnsiStringConstRef localname, SFXAnsiStringConstRef qname, SFXXMLGrammar::XMLAttrListConstRef attrList) { // display name of the element TRACE("element: %s start", qname.GetCString()); // get enumerator for first attribute of the element SFXXMLGrammar::XMLAttrList::Enumerator etor = attrList.GetFirstEnumerator(); // display each name and its value of attributes while (etor.HasNext()) { SFXXMLGrammar::LPXMLATTR attr = etor.GetNext(); // gete next attribute TRACE("attrname: %s", attr->_attName.GetCString()); // display name of attribute TRACE("attrvalue: %s", attr->_value.GetCString()); // display value of attribute } } // handler to receive the notification of the end of an element Void MyXMLHandler::EndElement(SFXAnsiStringConstRef uri, SFXAnsiStringConstRef localname, SFXAnsiStringConstRef qname) { TRACE("element: %s end", qname.GetCString()); } // handler to receive the notification of a character data Void MyXMLHandler::Characters(SFXAnsiStringConstRef string, BoolConst /*cdataSection*/) { TRACE("text: %s", string.GetCString()); return; } // following are empty handlers (must be defined even if event is ignored) // handler to receive the notification of a unnecessary white space Void MyXMLHandler::IgnorableWhitespace(SFXAnsiStringConstRef string) { return; } // handler to receive the notification of a processing instruction Void MyXMLHandler::ProcessingInstruction(SFXAnsiStringConstRef target, SFXAnsiStringConstRef data) { return; } // handler to receive the notification of the end of a mapping between prefix and namespace URI Void MyXMLHandler::EndPrefixMapping(SFXAnsiStringConstRef prefix) { return; } // handler to receive the notification of the start of a mapping between prefix and namespace URI Void MyXMLHandler::StartPrefixMapping(SFXAnsiStringConstRef prefix, SFXAnsiStringConstRef uri) { return; } // handler to receive the notification of a comment Void MyXMLHandler::Comment(SFXAnsiStringConstRef string) { return; } // handler to receive the notification of the end of a CDATA section Void MyXMLHandler::EndCDATA(Void) { return; } // handler to receive the notification of the end of a DTD declaration Void MyXMLHandler::EndDTD(Void) { return; } // handler to receive the notification of the end of an entity Void MyXMLHandler::EndEntity(SFXAnsiStringConstRef name) { return; } // handler to receive the notification of the start of a CDATA section Void MyXMLHandler::StartCDATA(Void) { return; } // handler to receive the notification of the start of a DTD declaration Void MyXMLHandler::StartDTD(SFXAnsiStringConstRef name, SFXAnsiStringConstRef publicId, SFXAnsiStringConstRef systemId) { return; } // handler to receive the notification of the start of an entity Void MyXMLHandler::StartEntity(SFXAnsiStringConstRef name) { return; } // handler to receive the notification of a notation declaration Void MyXMLHandler::NotationDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef publicId, SFXAnsiStringConstRef systemId) { return; } // handler to receive the notification of the declaration of an unparsed entity Void MyXMLHandler::UnparsedEntityDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef publicId, SFXAnsiStringConstRef systemId, SFXAnsiStringConstRef notationName) { return; } // handler to receive the notification of an element type declaration Void MyXMLHandler::ElementDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef model) { return; } // handler to receive the notification of an ATTLIST declaration Void MyXMLHandler::AttributeDecl(SFXAnsiStringConstRef ename, SFXAnsiStringConstRef aname, SFXAnsiStringConstRef type, SFXAnsiStringConstRef mode, SFXAnsiStringConstRef value) { return; } // handler to receive the notification of an internal entity declaration Void MyXMLHandler::InternalEntityDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef value) { return; } // handler to receive the notification of an external entity declaration Void MyXMLHandler::ExternalEntityDecl(SFXAnsiStringConstRef name, SFXAnsiStringConstRef publicId, SFXAnsiStringConstRef systemId) { return; } // handler to receive the notification of an error Void MyXMLHandler::ErrorReport(SFCErrorConst error, SFXAnsiStringConstRef errInfo) { return; }
Constructor/Destructor |
---|
SFXXMLDefaultHandler( Void ) Constructor of the SFXXMLDefaultHandler class.
|
~SFXXMLDefaultHandler( Void ) Destructor of the SFXXMLDefaultHandler class.
|
Public Functions | |
---|---|
Void |
AttributeDecl(
SFXAnsiStringConstRef ename
, SFXAnsiStringConstRef aname
, SFXAnsiStringConstRef type
, SFXAnsiStringConstRef valuedefault
, SFXAnsiStringConstRef value
) Receive the notification of an ATTLIST declaration.
|
Void |
Characters(
SFXAnsiStringConstRef string
, BoolConst cdataSection = true
) Receive the notification of a character data.
|
Void |
Comment(
SFXAnsiStringConstRef string
) Receive the notification of a comment.
|
Void |
ElementDecl(
SFXAnsiStringConstRef name
, SFXAnsiStringConstRef model
) Receive the notification of an element type declaration.
|
Void |
EndCDATA( Void ) Receive the notification of the end of a CDATA section.
|
Void |
EndDTD( Void ) Receive the notification of the end of a DTD declaration.
|
Void |
EndDocument( Void ) Receive the notification of the end of a document.
|
Void |
EndElement(
SFXAnsiStringConstRef uri = SFXAnsiString::EmptyInstance()
, SFXAnsiStringConstRef localname = SFXAnsiString::EmptyInstance()
, SFXAnsiStringConstRef qname = SFXAnsiString::EmptyInstance()
) Receive the notification of the end of an element.
|
Void |
EndEntity(
SFXAnsiStringConstRef name
) Receive the notification of the end of an entity.
|
Void |
EndPrefixMapping(
SFXAnsiStringConstRef prefix
) Receive the notification of the end of a mapping between prefix and namespace URI.
|
Void |
ErrorReport(
SFCErrorConst error
, SFXAnsiStringConstRef errInfo
) Recieve the notification of an error.
|
Void |
ExternalEntityDecl(
SFXAnsiStringConstRef name
, SFXAnsiStringConstRef publicId
, SFXAnsiStringConstRef systemId
) Receive the notification of an external entity declaration.
|
Void |
IgnorableWhitespace(
SFXAnsiStringConstRef string
) Receive the notification of a unnecessary white space.
|
Void |
InternalEntityDecl(
SFXAnsiStringConstRef name
, SFXAnsiStringConstRef value
) Receive the notification of an internal entity declaration.
|
Void |
NotationDecl(
SFXAnsiStringConstRef name
, SFXAnsiStringConstRef publicId
, SFXAnsiStringConstRef systemId
) Receive the notification of a notation declaration.
|
Void |
ProcessingInstruction(
SFXAnsiStringConstRef target
, SFXAnsiStringConstRef data
) Receive the notification of a processing instruction.
|
Void |
StartCDATA( Void ) Receive the notification of the start of a CDATA section.
|
Void |
StartDTD(
SFXAnsiStringConstRef name
, SFXAnsiStringConstRef publicId
, SFXAnsiStringConstRef systemId
) Receive the notification of the start of a DTD declaration.
|
Void |
StartDocument( Void ) Receive the notification of the start of a document.
|
Void |
StartElement(
SFXAnsiStringConstRef uri
, SFXAnsiStringConstRef localname
, SFXAnsiStringConstRef qname
, SFXXMLGrammar::XMLAttrListConstRef attrList
) Receive the notification of the start of an element.
|
Void |
StartEntity(
SFXAnsiStringConstRef name
) Receive the notification of the start of an entity.
|
Void |
StartPrefixMapping(
SFXAnsiStringConstRef prefix
, SFXAnsiStringConstRef uri
) Receive the notification of the start of a mapping between prefix and namespace URI.
|
Void |
UnparsedEntityDecl(
SFXAnsiStringConstRef name
, SFXAnsiStringConstRef publicId
, SFXAnsiStringConstRef systemId
, SFXAnsiStringConstRef notationName
) Receive the notification of the declaration of an unparsed entity.
|
[ public, explicit ] SFXXMLDefaultHandler(Void);
[ public, virtual ] virtual ~SFXXMLDefaultHandler(Void);
[ public, virtual ] Void AttributeDecl( SFXAnsiStringConstRef ename // name of the element SFXAnsiStringConstRef aname // name of attribute SFXAnsiStringConstRef type // type of attribute SFXAnsiStringConstRef valuedefault // default value of attribute SFXAnsiStringConstRef value // default mode of attribute: #IMPLIED, #REQUIRED, or #FIXED );
[ public, virtual ] Void Characters( SFXAnsiStringConstRef string // XML document BoolConst cdataSection = true // whether the string argument is CDATASection or not );
XML document.
In case of the SAX parser, "true" is always specified.
The SAX parser calls this function to notify of having received character data.
[ public, virtual ] Void Comment( SFXAnsiStringConstRef string // comment );
The SFXXMLDefaultHandler::Comment function reveives the notification of a comment in the documents including the external DTD.
[ public, virtual ] Void ElementDecl( SFXAnsiStringConstRef name // element type name SFXAnsiStringConstRef model // normalized content mode );
About Normalized Content Model: | |
---|---|
Parameter entities are expanded and unnecessary space characters are removed. |
[ public, virtual ] Void EndCDATA(Void);
[ public, virtual ] Void EndDTD(Void);
Note | |
---|---|
The SFXXMLDefaultHandler::EndDTD function is not called when the DOCTYPE declaration is not included in the document. |
[ public, virtual ] Void EndDocument(Void);
[ public, virtual ] Void EndElement( SFXAnsiStringConstRef uri = SFXAnsiString::EmptyInstance() // namespace URI or an empty string SFXAnsiStringConstRef localname = SFXAnsiString::EmptyInstance() // local name (without prefix), or an empty string SFXAnsiStringConstRef qname = SFXAnsiString::EmptyInstance() // qualified name (with prefix), or an empty string );
The SAX parser calls this method at the end of each element in the XML document. For an EndElement event, there is a corresponding StartElement event. If the element is empty, StartElement event and EndElement event are the same.
[ public, virtual ] Void EndEntity( SFXAnsiStringConstRef name // entity name );
[ public, virtual ] Void EndPrefixMapping( SFXAnsiStringConstRef prefix // prefix being mapped );
[ public, virtual ] Void ErrorReport( SFCErrorConst error // error code SFXAnsiStringConstRef errInfo // message of error code );
The SFXXMLDefaultHandler::ErrorReport function receives the notification of an error such as an XML parsing error, a DTD or Schema validation error, an insufficient memory error, ect.
[ public, virtual ] Void ExternalEntityDecl( SFXAnsiStringConstRef name // entity name SFXAnsiStringConstRef publicId // public identification of entity SFXAnsiStringConstRef systemId // system identifier of entity );
[ public, virtual ] Void IgnorableWhitespace( SFXAnsiStringConstRef string // unnecessary white space );
When parsing the indent, notify a unnecessary white space character in the element using the SFXXMLDefaultHandler::IgnorableWhitespace function.
[ public, virtual ] Void InternalEntityDecl( SFXAnsiStringConstRef name // entity name SFXAnsiStringConstRef value // content of entity );
[ public, virtual ] Void NotationDecl( SFXAnsiStringConstRef name // notation name SFXAnsiStringConstRef publicId // public identifier of notation SFXAnsiStringConstRef systemId // system identifier of notation );
[ public, virtual ] Void ProcessingInstruction( SFXAnsiStringConstRef target // target of processing instruction SFXAnsiStringConstRef data // content of processing instruction excluding the target );
[ public, virtual ] Void StartCDATA(Void);
The content of a CDATA section is passed to the SFXXMLDefaultHandler::Characters function. The SFXXMLDefaultHandler::StartCDATA receives only the notification of the starting boundary of a CDATA section.
[ public, virtual ] Void StartDTD( SFXAnsiStringConstRef name // document type name SFXAnsiStringConstRef publicId // public identifier of external DTD subset SFXAnsiStringConstRef systemId // system identifier of external DTD subset );
The SFXXMLDefaultHandler::StartDTD function is not called if the DOCTYPE declaration is not included in the document. The StartDTD and EndDTD events occur before the first StartElement event between the StartDocument and EndDocument events.
[ public, virtual ] Void StartDocument(Void);
[ public, virtual ] Void StartElement( SFXAnsiStringConstRef uri // namespace URI of the element SFXAnsiStringConstRef localname // local name (without prefix) SFXAnsiStringConstRef qname // qualified name (with prefix) SFXXMLGrammar::XMLAttrListConstRef attrList // list of attributes );
The SAX parser calls the SFXXMLDefaultHandler::StartElement function before each element in the XML document is processed. For each StartElement event, there exists a corresponding EndElement event. Even if the element is empty, it is the same.
All contents of the element are sequentially notified before the corresponding EndElement event.
[ public, virtual ] Void StartEntity( SFXAnsiStringConstRef name // name of entity );
In case of a parameter entity, entity name starts with '%'.
In case of an external DTD, entity name starts with '[dtd]'.
An entity is nomalized and then notified to the handler.
[ public, virtual ] Void StartPrefixMapping( SFXAnsiStringConstRef prefix // qualified name (with prefix) SFXAnsiStringConstRef uri // namespace URI );
[ public, virtual ] Void UnparsedEntityDecl( SFXAnsiStringConstRef name // name of entity SFXAnsiStringConstRef publicId // public identifier of entity SFXAnsiStringConstRef systemId // system identifier of entity SFXAnsiStringConstRef notationName // name of notation );
The notation name matches the notation notified by the NotationDecl event. To refer to the entity later, save it somewhere.
An unparsed entity may be represented as an attribute value.
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |