Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:13.5 xml.parsers.expatUp:13.5 xml.parsers.expatNext:13.5.2 ExpatError Exceptions

 
13.5.1 XMLParser Objects

xmlparser objects have the following methods:

Parse(data[, isfinal])
Parses the contents of the stringdata, calling the appropriatehandler functions to process the parsed data.isfinal must betrue on the final call to this method.data can be the emptystring at any time.

ParseFile(file)
Parse XML data reading from the objectfile.file onlyneeds to provide theread(nbytes) method, returning theempty string when there's no more data.

SetBase(base)
Sets the base to be used for resolving relative URIs in systemidentifiers in declarations. Resolving relative identifiers is leftto the application: this value will be passed through as thebase argument to theExternalEntityRefHandler,NotationDeclHandler, andUnparsedEntityDeclHandler functions.

GetBase()
Returns a string containing the base set by a previous call toSetBase(), orNone ifSetBase() hasn't been called.

GetInputContext()
Returns the input data that generated the current event as a string.The data is in the encoding of the entity which contains the text.When called while an event handler is not active, the return value isNone.New in version 2.1.

ExternalEntityParserCreate(context[, encoding])
Create a ``child'' parser which can be used to parse an externalparsed entity referred to by content parsed by the parent parser. Thecontext parameter should be the string passed to theExternalEntityRefHandler() handler function, described below.The child parser is created with theordered_attributes,returns_unicode andspecified_attributes set to thevalues of this parser.

xmlparser objects have the following attributes:

ordered_attributes
Setting this attribute to a non-zero integer causes the attributes tobe reported as a list rather than a dictionary. The attributes arepresented in the order found in the document text. For eachattribute, two list entries are presented: the attribute name and theattribute value. (Older versions of this module also used thisformat.) By default, this attribute is false; it may be changed atany time.New in version 2.1.

returns_unicode
If this attribute is set to a non-zero integer, the handler functionswill be passed Unicode strings. Ifreturns_unicode is 0,8-bit strings containing UTF-8 encoded data will be passed to thehandlers.Changed in version 1.6:Can be changed at any time to affect the result type.

specified_attributes
If set to a non-zero integer, the parser will report only thoseattributes which were specified in the document instance and not thosewhich were derived from attribute declarations. Applications whichset this need to be especially careful to use what additionalinformation is available from the declarations as needed to complywith the standards for the behavior of XML processors. By default,this attribute is false; it may be changed at any time.New in version 2.1.

The following attributes contain values relating to the most recenterror encountered by anxmlparser object, and will only havecorrect values once a call toParse() orParseFile()has raised axml.parsers.expat.ExpatError exception.

ErrorByteIndex
Byte index at which an error occurred.

ErrorCode
Numeric code specifying the problem. This value can be passed to theErrorString() function, or compared to one of the constantsdefined in theerrors object.

ErrorColumnNumber
Column number at which an error occurred.

ErrorLineNumber
Line number at which an error occurred.

Here is the list of handlers that can be set. To set a handler on anxmlparser objecto, useo.handlername =func.handlername mustbe taken from the following list, andfunc must be a callableobject accepting the correct number of arguments. The arguments areall strings, unless otherwise stated.

XmlDeclHandler(version, encoding, standalone)
Called when the XML declaration is parsed. The XML declaration is the(optional) declaration of the applicable version of the XMLrecommendation, the encoding of the document text, and an optional``standalone'' declaration.version andencoding will bestrings of the type dictated by thereturns_unicodeattribute, andstandalone will be1 if the document isdeclared standalone,0 if it is declared not to be standalone,or-1 if the standalone clause was omitted.This is only available with Expat version 1.95.0 or newer.New in version 2.1.

StartDoctypeDeclHandler(doctypeName, systemId, publicId, has_internal_subset)
Called when Expat begins parsing the document type declaration(<!DOCTYPE ...). ThedoctypeName is provided exactlyas presented. ThesystemId andpublicId parameters givethe system and public identifiers if specified, orNone ifomitted.has_internal_subset will be true if the documentcontains and internal document declaration subset.This requires Expat version 1.2 or newer.

EndDoctypeDeclHandler()
Called when Expat is done parsing the document type delaration.This requires Expat version 1.2 or newer.

ElementDeclHandler(name, model)
Called once for each element type declaration.name is the nameof the element type, andmodel is a representation of thecontent model.

AttlistDeclHandler(elname, attname, type, default, required)
Called for each declared attribute for an element type. If anattribute list declaration declares three attributes, this handler iscalled three times, once for each attribute.elname is the nameof the element to which the declaration applies andattname isthe name of the attribute declared. The attribute type is a stringpassed astype; the possible values are'CDATA','ID','IDREF', ...default gives the default value for the attribute used when theattribute is not specified by the document instance, orNone ifthere is no default value (#IMPLIED values). If the attributeis required to be given in the document instance,required willbe true.This requires Expat version 1.95.0 or newer.

StartElementHandler(name, attributes)
Called for the start of every element.name is a stringcontaining the element name, andattributes is a dictionarymapping attribute names to their values.

EndElementHandler(name)
Called for the end of every element.

ProcessingInstructionHandler(target, data)
Called for every processing instruction.

CharacterDataHandler(data)
Called for character data. This will be called for normal characterdata, CDATA marked content, and ignorable whitespace. Applicationswhich must distinguish these cases can use theStartCdataSectionHandler,EndCdataSectionHandler,andElementDeclHandler callbacks to collect the requiredinformation.

UnparsedEntityDeclHandler(entityName, base, systemId, publicId, notationName)
Called for unparsed (NDATA) entity declarations. This is only presentfor version 1.2 of the Expat library; for more recent versions, useEntityDeclHandler instead. (The underlying function in theExpat library has been declared obsolete.)

EntityDeclHandler(entityName, is_parameter_entity, value, base, systemId, publicId, notationName)
Called for all entity declarations. For parameter and internalentities,value will be a string giving the declared contentsof the entity; this will beNone for external entities. ThenotationName parameter will beNone for parsed entities,and the name of the notation for unparsed entities.is_parameter_entity will be true if the entity is a paremeterentity or false for general entities (most applications only need tobe concerned with general entities).This is only available starting with version 1.95.0 of the Expatlibrary.New in version 2.1.

NotationDeclHandler(notationName, base, systemId, publicId)
Called for notation declarations.notationName,base, andsystemId, andpublicId are strings if given. If thepublic identifier is omitted,publicId will beNone.

StartNamespaceDeclHandler(prefix, uri)
Called when an element contains a namespace declaration. Namespacedeclarations are processed before theStartElementHandler iscalled for the element on which declarations are placed.

EndNamespaceDeclHandler(prefix)
Called when the closing tag is reached for an element that contained a namespace declaration. This is called once for eachnamespace declaration on the element in the reverse of the order forwhich theStartNamespaceDeclHandler was called to indicatethe start of each namespace declaration's scope. Calls to thishandler are made after the correspondingEndElementHandlerfor the end of the element.

CommentHandler(data)
Called for comments.data is the text of the comment, excludingthe leading `<!--' and trailing `-->'.

StartCdataSectionHandler()
Called at the start of a CDATA section. This andStartCdataSectionHandler are needed to be able to identifythe syntactical start and end for CDATA sections.

EndCdataSectionHandler()
Called at the end of a CDATA section.

DefaultHandler(data)
Called for any characters in the XML document forwhich no applicable handler has been specified. This meanscharacters that are part of a construct which could be reported, butfor which no handler has been supplied.

DefaultHandlerExpand(data)
This is the same as theDefaultHandler, but doesn't inhibit expansion of internal entities.The entity reference will not be passed to the default handler.

NotStandaloneHandler()
Called if theXML document hasn't been declared as being a standalone document.This happens when there is an external subset or a reference to aparameter entity, but the XML declaration does not set standalone toyes in an XML declaration. If this handler returns0,then the parser will throw anXML_ERROR_NOT_STANDALONEerror. If this handler is not set, no exception is raised by theparser for this condition.

ExternalEntityRefHandler(context, base, systemId, publicId)
Called for references to external entities.base is the currentbase, as set by a previous call toSetBase(). The public andsystem identifiers,systemId andpublicId, are strings ifgiven; if the public identifier is not given,publicId will beNone. Thecontext value is opaque and should only beused as described below.

For external entities to be parsed, this handler must be implemented.It is responsible for creating the sub-parser usingExternalEntityParserCreate(context), initializing it withthe appropriate callbacks, and parsing the entity. This handlershould return an integer; if it returns0, the parser willthrow anXML_ERROR_EXTERNAL_ENTITY_HANDLING error,otherwise parsing will continue.

If this handler is not provided, external entities are reported by theDefaultHandler callback, if provided.


Previous PageUp One LevelNext PagePython Library ReferenceContentsModule IndexIndex
Previous:13.5 xml.parsers.expatUp:13.5 xml.parsers.expatNext:13.5.2 ExpatError Exceptions
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp