PrevNextUpHome SophiaFramework UNIVERSE 5.3

19.3. DOM and SAX

There are two kinds of XML parsers. One is named "DOM" parser, the another is named "SAX" parser.

The DOM parser expands XML document onto memory as the DOM tree. With the DOM parser, it is very easy to operate an XML document, but usually a lot of memory is needed.

The SAX parser is a set of APIs that read XML document sequentially from the beginning and processes it in an event driven style. It needs less memory and is faster than the DOM parser, but there is no update function for XML documents such as adding, deleting a node etc.

SophiaFramework provides you with SFXXMLDOMParser as the DOM parser, SFXXMLSAXParser as the SAX parser.

19.3.1. Basic Function of SFXXMLDOMParser and SFXXMLSAXParser

The basic functions of SFXXMLDOMParser and SFXXMLSAXParser used by SophiaFramework are as follows:

Table 19.6. Parse

Function Name of the SFXXMLDOMParser class Function Name of the SFXXMLSAXParser class Description
SFXXMLDOMParser::Parse SFXXMLSAXParser::Parse Function to read and parse XML document.

Set a parser attribute

Parser attributes are set by using the Set function.

Table 19.7. Function of Setting Parser Attributes

Function Name of the SFXXMLDOMParser class Function Name of the SFXXMLSAXParser class Description
SFXXMLDOMParser::SetGrammar SFXXMLSAXParser::SetGrammar Set the grammar used by the parser.
SFXXMLDOMParser::SetDoNamespaces SFXXMLSAXParser::SetDoNamespaces Set the flag to process the namespace.
SFXXMLDOMParser::SetDoSchema SFXXMLSAXParser::SetDoSchema Set the flag to process the schema.
SFXXMLDOMParser::SetSchemaLocation SFXXMLSAXParser::SetSchemaLocation Set the XSD file to load.
SFXXMLDOMParser::SetValidationSchema SFXXMLSAXParser::SetValidationSchema Set the flag to validate the XML document with the schema.
SFXXMLDOMParser::SetLoadExternalDTD SFXXMLSAXParser::SetLoadExternalDTD Set the flag to load the external DTD file.
SFXXMLDOMParser::SetValidationDTD SFXXMLSAXParser::SetValidationDTD Set the flag to validate the XML document with the DTD.
SFXXMLDOMParser::SetCreateCommentNodes ------ Set the flag to generate the comment node.
SFXXMLDOMParser::SetDoIndent SFXXMLSAXParser::SetDoIndent Set the flag to indent the XML document.
[Caution] caution

In these SetXXXXXXXXX functions, the attribute value is set to "false" by default except for the SFXXMLDOMParser::SetIgnoreAnnotations / SFXXMLSAXParser::SetIgnoreAnnotations function.

Get a parser attribute

Parser attributes can be obtained by using Get functions.

Table 19.8. Function of Getting Parser Attributes

Function Name of the SFXXMLDOMParser class Function Name of the SFXXMLSAXParser class Description
SFXXMLDOMParser::GetDoNamespaces SFXXMLSAXParser::GetDoNamespaces Get the value of the flag to process the namespace.
SFXXMLDOMParser::GetDoSchema SFXXMLSAXParser::GetDoSchema Get the value of the flag to process the schema.
SFXXMLDOMParser::GetValidationSchema SFXXMLSAXParser::GetValidationSchema Get the value of the flag to validate the XML document with the schema.
SFXXMLDOMParser::GetLoadExternalDTD SFXXMLSAXParser::GetLoadExternalDTD Get the value of the flag to load the external DTD file.
SFXXMLDOMParser::GetValidationDTD SFXXMLSAXParser::GetValidationDTD Get the value of the flag to validate the XML document with the DTD.
SFXXMLDOMParser::GetDocument ------ Get the root node of the DOM tree.
SFXXMLDOMParser::GetCreateCommentNodes ------ Get the value of the flag to generate the comment node.
SFXXMLDOMParser::GetDoIndent SFXXMLSAXParser::GetDoIndent Get the value of the flag to indent the XML document.

Helper function

Table 19.9. Helper function

Function Name of the SFXXMLDOMParser class Description
SFXXMLDOMParser::GetFirstChildElement Get the first child Element node of the Element node with the specified name.
SFXXMLDOMParser::GetFirstChildElementNS Get the first child Element node of the Element node with the specified namespace URI and local name.
SFXXMLDOMParser::GetNextSiblingElement Get the next sibling Element node of the Element node with the specified name.
SFXXMLDOMParser::GetNextSiblingElementNS Get the next sibling Element node of the Element node with the specified namespace URI and local name.
[Note] Helper functions

Only available for SFXXMLDOMParser, not available for SFXXMLSAXParser

[Caution] Function of Parsing with SFXXMLSAXParser

To parse by using SFXXMLSAXParser, it is necessary to inherit from the SFXXMLDefaultHandler class for setting a handler interface to notify events.