PrevNextUpHome SophiaFramework UNIVERSE 5.3
SFXXMLDefaultHandler
[XML] Base class for implementing the event handlers for the SAX Parser.
#include <SFXXMLDefaultHandler.hpp>
class SFXXMLDefaultHandler;
SFMTYPEDEFCLASS(SFXXMLDefaultHandler)

Description

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;
}

Member

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.

SFXXMLDefaultHandler::SFXXMLDefaultHandler
Constructor of the SFXXMLDefaultHandler class.
[ public, explicit ]
SFXXMLDefaultHandler(Void);

SFXXMLDefaultHandler::~SFXXMLDefaultHandler
Destructor of the SFXXMLDefaultHandler class.
[ public, virtual ]
virtual ~SFXXMLDefaultHandler(Void);

SFXXMLDefaultHandler::AttributeDecl
Receive the notification of an ATTLIST declaration.
[ 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
);

SFXXMLDefaultHandler::Characters
Receive the notification of a character data.
[ public, virtual ]
Void Characters(
    SFXAnsiStringConstRef string    // XML document
    BoolConst cdataSection = true   // whether the string argument is CDATASection or not
);

Arguments

string

XML document.

cdataSection

In case of the SAX parser, "true" is always specified.

Description

The SAX parser calls this function to notify of having received character data.


SFXXMLDefaultHandler::Comment
Receive the notification of a comment.
[ public, virtual ]
Void Comment(
    SFXAnsiStringConstRef string   // comment
);

Description

The SFXXMLDefaultHandler::Comment function reveives the notification of a comment in the documents including the external DTD.


SFXXMLDefaultHandler::ElementDecl
Receive the notification of an element type declaration.
[ public, virtual ]
Void ElementDecl(
    SFXAnsiStringConstRef name    // element type name
    SFXAnsiStringConstRef model   // normalized content mode
);

Description

[Note] About Normalized Content Model:

Parameter entities are expanded and unnecessary space characters are removed.


SFXXMLDefaultHandler::EndCDATA
Receive the notification of the end of a CDATA section.
[ public, virtual ]
Void EndCDATA(Void);

Reference

SFXXMLDefaultHandler::StartCDATA


SFXXMLDefaultHandler::EndDTD
Receive the notification of the end of a DTD declaration.
[ public, virtual ]
Void EndDTD(Void);

Description

[Note] Note
The SFXXMLDefaultHandler::EndDTD function is not called when the DOCTYPE declaration is not included in the document.

Reference

SFXXMLDefaultHandler::StartDTD


SFXXMLDefaultHandler::EndDocument
Receive the notification of the end of a document.
[ public, virtual ]
Void EndDocument(Void);

Reference

SFXXMLDefaultHandler::StartDocument


SFXXMLDefaultHandler::EndElement
Receive the notification of the end of an element.
[ 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
);

Description

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.

Reference

SFXXMLDefaultHandler::StartElement


SFXXMLDefaultHandler::EndEntity
Receive the notification of the end of an entity.
[ public, virtual ]
Void EndEntity(
    SFXAnsiStringConstRef name   // entity name
);

Reference

SFXXMLDefaultHandler::StartEntity


SFXXMLDefaultHandler::EndPrefixMapping
Receive the notification of the end of a mapping between prefix and namespace URI.
[ public, virtual ]
Void EndPrefixMapping(
    SFXAnsiStringConstRef prefix   // prefix being mapped
);

Reference

SFXXMLDefaultHandler::StartPrefixMapping


SFXXMLDefaultHandler::ErrorReport
Recieve the notification of an error.
[ public, virtual ]
Void ErrorReport(
    SFCErrorConst error             // error code
    SFXAnsiStringConstRef errInfo   // message of error code 
);

Description

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.


SFXXMLDefaultHandler::ExternalEntityDecl
Receive the notification of an external entity declaration.
[ public, virtual ]
Void ExternalEntityDecl(
    SFXAnsiStringConstRef name       // entity name
    SFXAnsiStringConstRef publicId   // public identification of entity
    SFXAnsiStringConstRef systemId   // system identifier of entity
);

SFXXMLDefaultHandler::IgnorableWhitespace
Receive the notification of a unnecessary white space.
[ public, virtual ]
Void IgnorableWhitespace(
    SFXAnsiStringConstRef string   // unnecessary white space
);

Description

When parsing the indent, notify a unnecessary white space character in the element using the SFXXMLDefaultHandler::IgnorableWhitespace function.


SFXXMLDefaultHandler::InternalEntityDecl
Receive the notification of an internal entity declaration.
[ public, virtual ]
Void InternalEntityDecl(
    SFXAnsiStringConstRef name    // entity name
    SFXAnsiStringConstRef value   // content of entity
);

SFXXMLDefaultHandler::NotationDecl
Receive the notification of a notation declaration.
[ public, virtual ]
Void NotationDecl(
    SFXAnsiStringConstRef name       // notation name
    SFXAnsiStringConstRef publicId   // public identifier of notation
    SFXAnsiStringConstRef systemId   // system identifier of notation
);

Reference

SFXXMLDefaultHandler::UnparsedEntityDecl


SFXXMLDefaultHandler::ProcessingInstruction
Receive the notification of a processing instruction.
[ public, virtual ]
Void ProcessingInstruction(
    SFXAnsiStringConstRef target   // target of processing instruction
    SFXAnsiStringConstRef data     // content of processing instruction excluding the target
);

SFXXMLDefaultHandler::StartCDATA
Receive the notification of the start of a CDATA section.
[ public, virtual ]
Void StartCDATA(Void);

Description

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.

Reference

SFXXMLDefaultHandler::Characters | SFXXMLDefaultHandler::EndCDATA


SFXXMLDefaultHandler::StartDTD
Receive the notification of the start of a DTD declaration.
[ 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
);

Description

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.

Reference

SFXXMLDefaultHandler::EndDTD


SFXXMLDefaultHandler::StartDocument
Receive the notification of the start of a document.
[ public, virtual ]
Void StartDocument(Void);

Reference

SFXXMLDefaultHandler::EndDocument


SFXXMLDefaultHandler::StartElement
Receive the notification of the start of an element.
[ 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
);

Description

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.

Reference

SFXXMLDefaultHandler::EndElement


SFXXMLDefaultHandler::StartEntity
Receive the notification of the start of an entity.
[ public, virtual ]
Void StartEntity(
    SFXAnsiStringConstRef name   // name of entity
);

Argument

name

In case of a parameter entity, entity name starts with '%'.

In case of an external DTD, entity name starts with '[dtd]'.

Description

An entity is nomalized and then notified to the handler.

Reference

SFXXMLDefaultHandler::EndEntity


SFXXMLDefaultHandler::StartPrefixMapping
Receive the notification of the start of a mapping between prefix and namespace URI.
[ public, virtual ]
Void StartPrefixMapping(
    SFXAnsiStringConstRef prefix   // qualified name (with prefix) 
    SFXAnsiStringConstRef uri      // namespace URI
);

Reference

SFXXMLDefaultHandler::EndPrefixMapping


SFXXMLDefaultHandler::UnparsedEntityDecl
Receive the notification of the declaration of an unparsed entity.
[ 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 
);

Description

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.

Reference

SFXXMLDefaultHandler::NotationDecl