ContentHandler
The ContentHandler
is
the most used of the SAX handler objects. It is this handler that
receives information about the elements being parsed, their
attributes, and the content between start and end tags. The parse
and parseString
convenience functions described
in the previous section require a ContentHandler
implementation to be passed
in.
setDocumentLocator(
locator
)
SAX parsers are encouraged to supply a
locator
for finding the origin of
events within the document, so that you can determine the end
position of any document-related event. The
locator
object conforms to the
Locator
interface, described later in this Appendix.
startDocument( )
When the parser begins a document, it calls this
method first and only once, with the exception of setDocumentLocator
, which is called
first if implemented.
endDocument( )
This method is called only once as the very last method invoked by the parser.
startPrefixMapping(
prefix, uri
)
This method is called when a namespace declaration
is encountered. The prefix
parameter
is the prefix string used in the document, and
uri
refers to the Universal Resource
Indicator (URI) that the prefix represents.
endPrefixMapping(
prefix
)
This event occurs at the time the end element with
the corresponding startPrefixMapping
is called,
indicating that the declaration for
prefix
has gone out of scope. It does
not indicate that there is no mapping for
prefix
; an outer declaration may
still be in scope.
startElement(
name, attrs
)
This event is called each time an element’s
opening tag is encountered. The name is the actual tag name of
the element. The attrs
parameter is a
Python dictionary containing attribute names and values. This is
called when Namespace processing is not being used.
startElementNS(
name, qname, attrs
)
In this method, name
is
a tuple containing the URI and localname. The
qname
parameter is the actual tag
name used in the XML document. The
attrs
variable is once again the
Python dictionary containing the attribute name and value pairs.
This is called only when Namespaces are being used.
endElement(
name
)
This event is called when the end of an element is
found, if the start of the element was reported by startElement
.
endElementNS(
name, qname
)
This is the corresponding method and event for
startElementNS
event.
characters(
content
)
This method is called for each section of
character data that is not part of an element or other markup.
This method may be called multiple times in what might appear to
be a contiguous section of character data. In other words, some
parsers may call this method for each line of the document, or
some may choose to aggregate lines and call the method between
other sections of markup. Either way, all character data is
passed through this method call. The
content
parameter contains the actual
character data.
ignorableWhitespace(
whitespace
)
This method is called by validating parsers when
reporting whitespace in element content. Similar to the characters
method mentioned earlier,
this might be called multiple times depending on the parser.
Validating parsers must call this method. The
whitespace
parameter contains the
actual whitespace encountered.
processingInstruction(
target, data
)
This method is called once for each processing
instruction found by the parser. XML declarations and text
declarations must not be reported in this fashion. Both the
target
and
data
parameters are string types
containing the processing instruction’s relevant information to
the application.
skippedEntity(
name
)
The parser calls this method each time an entity reference is found and the content of the entity is not parsed. Nonvalidating parsers may skip external entities and any entities for which they have not seen a declaration.
18.117.71.211