Package gavo :: Package utils :: Module plainxml :: Class StartEndHandler
[frames] | no frames]

Class StartEndHandler

source code

xml.sax.handler.ContentHandler --+
                                 |
                                StartEndHandler
Known Subclasses:

This class provides startElement, endElement and characters methods that translate events into method calls.

When an opening tag is seen, we look of a _start_<element name> method and, if present, call it with the name and the attributes. When a closing tag is seen, we try to call _end_<element name> with name, attributes and contents. If the _end_xxx method returns a string (or similar), this value will be added to the content of the enclosing element.

Rather than overriding __init__, you probably want to override the _initialize() method to create the data structures you want to fill from XML.

StartEndHandlers clean element names from namespace prefixes, and they ignore them in every other way. If you need namespaces, use a different interface.

Instance Methods
 
__init__(self) source code
 
characters(self, chars)
Receive notification of character data.
source code
 
cleanupName(self, name) source code
 
endElement(self, name, suppress=False)
Signals the end of an element in non-namespace mode.
source code
 
endElementNS(self, namePair, qName)
Signals the end of an element in namespace mode.
source code
 
getAttrsAsDict(self, attrs)
returns attrs as received from SAX as a dictionary.
source code
 
getParentTag(self, depth=1)
Returns the name of the parent element.
source code
 
getResult(self) source code
 
parse(self, stream) source code
 
parseString(self, string) source code
 
processingInstruction(self, target, data)
Receive notification of a processing instruction.
source code
 
setDocumentLocator(self, locator)
Called by the parser to give the application a locator for locating the origin of document events.
source code
 
startElement(self, name, attrs)
Signals the start of an element in non-namespace mode.
source code
 
startElementNS(self, namePair, qName, attrs)
Signals the start of an element in namespace mode.
source code

Inherited from xml.sax.handler.ContentHandler: endDocument, endPrefixMapping, ignorableWhitespace, skippedEntity, startDocument, startPrefixMapping

Method Details

__init__(self)
(Constructor)

source code 
Overrides: xml.sax.handler.ContentHandler.__init__

characters(self, chars)

source code 

Receive notification of character data.

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity so that the Locator provides useful information.

Overrides: xml.sax.handler.ContentHandler.characters
(inherited documentation)

endElement(self, name, suppress=False)

source code 

Signals the end of an element in non-namespace mode.

The name parameter contains the name of the element type, just as with the startElement event.

Overrides: xml.sax.handler.ContentHandler.endElement
(inherited documentation)

endElementNS(self, namePair, qName)

source code 

Signals the end of an element in namespace mode.

The name parameter contains the name of the element type, just as with the startElementNS event.

Overrides: xml.sax.handler.ContentHandler.endElementNS
(inherited documentation)

getAttrsAsDict(self, attrs)

source code 

returns attrs as received from SAX as a dictionary.

The main selling point is that any namespace prefixes are removed from the attribute names. Any prefixes on attrs remain, though.

getParentTag(self, depth=1)

source code 

Returns the name of the parent element.

This only works as written here in end handlers. In start handlers, you have to path depth=2 (since their tag already is on the stack.

processingInstruction(self, target, data)

source code 

Receive notification of a processing instruction.

The Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element.

A SAX parser should never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.

Overrides: xml.sax.handler.ContentHandler.processingInstruction
(inherited documentation)

setDocumentLocator(self, locator)

source code 

Called by the parser to give the application a locator for locating the origin of document events.

SAX parsers are strongly encouraged (though not absolutely required) to supply a locator: if it does so, it must supply the locator to the application by invoking this method before invoking any of the other methods in the DocumentHandler interface.

The locator allows the application to determine the end position of any document-related event, even if the parser is not reporting an error. Typically, the application will use this information for reporting its own errors (such as character content that does not match an application's business rules). The information returned by the locator is probably not sufficient for use with a search engine.

Note that the locator will return correct information only during the invocation of the events in this interface. The application should not attempt to use it at any other time.

Overrides: xml.sax.handler.ContentHandler.setDocumentLocator
(inherited documentation)

startElement(self, name, attrs)

source code 

Signals the start of an element in non-namespace mode.

The name parameter contains the raw XML 1.0 name of the element type as a string and the attrs parameter holds an instance of the Attributes class containing the attributes of the element.

Overrides: xml.sax.handler.ContentHandler.startElement
(inherited documentation)

startElementNS(self, namePair, qName, attrs)

source code 

Signals the start of an element in namespace mode.

The name parameter contains the name of the element type as a (uri, localname) tuple, the qname parameter the raw XML 1.0 name used in the source document, and the attrs parameter holds an instance of the Attributes class containing the attributes of the element.

The uri part of the name tuple is None for elements which have no namespace.

Overrides: xml.sax.handler.ContentHandler.startElementNS
(inherited documentation)