亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? xml-sax.txt

?? python web programming 部分
?? TXT
?? 第 1 頁 / 共 2 頁
字號:
Author: David Beazley (beazley@cs.uchicago.edu)

Python and SAX
==============

This chapter provides details on Python's support for SAX (Simple API
for XML).  SAX is a widely used interface specification for parsing
and processing XML.  Many high level XML processing modules use SAX
for their internal processing and there are many reasons why you might
want to use it yourself.  An introduction to XML parsing is provided
in the previous chapter and is not repeated here.  Instead, this
chapter builds upon that material and is aimed to be more of a SAX
reference.

Introduction to SAX
-------------------
First, SAX is not a package that you download and install. Instead, it
is a common API that is used to manipulate XML.  SAX is based on an
event model in which XML documents are scanned sequentially and
different handler methods are invoked for different document features.
This style of XML processing was already illustrated in the previous
section chapter describing the Expat module.  SAX merely generalizes
that approach and makes it more widely applicable.

The big picture of SAX is that subdivides XML processing into a 
collection of different tasks--each defined by a special interface.
In Python, these interfaces are implemented by a collection of
classes.  Figure 1 illustrates the different pieces:


                                      +--> ContentHandler     
                                      |        
          Raw Data        SAX Events  |--> DTDHandler
InputSource --> XMLReader ----------->+                        
                                      |--> EntityResolver
                                      |    
                                      +--> ErrorHandler
                                          

At the lowest level, XML data is read from a byte-stream that is
encapsulated by a special InputSource object.  This is usually just a
wrapper around an ordinary file.  The data read from an InputSource
object is then fed into an XMLReader object.  An XMLReader is just a
generalized parsing interface that sits on top of a low-level XML parser
such as Expat or xmlproc.   The XMLReader then sends different events
(SAX events) on to a collection of four different types of handler
objects.   The handlers are used to handle different document
features. For example:

 ContentHandler     -  Ordinary document text, elements, and attributes.

 DTDHandler         -  DTD declarations needed to parse the rest of
                       the document (unparsed external entities)
 
 EntityResolver     -  Handling of external entities.

 ErrorHandler       -  Error handling

In addition, SAX defines a number of miscellaneous interfaces
that are used during processing. For instance, a special Locator
object is used to keep track of the current parsing location and
an AttributeImpl object is used to store attributes.

Since SAX processing is essentially divided into two parts; parsing
and handling, two different Python modules contain most of the
functionality.  The xml.sax.xmlreader module contains the classes
pertaining to the parsing of XML and includes the InputSource,
XMLReader, Locator, and AttributeImpl classes.  The xml.sax.handler
module contains the classes for handling different SAX events and
includes the ContentHandler, DTDHandler, EntityResolver, and
ErrorHandler classes.  More generally, you will use the
xml.sax.xmlreader module to set up a parser.  You will then use the
xml.sax.handler module to implement the actions performed during
parsing.

The rest of this chapter documents the different SAX interfaces
and how they are used.

XML Readers
-----------
To read XML documents with SAX, you have first have to instantiate a
special XMLReader object.  An XMLReader is really nothing more than a
standardized wrapper around an existing XML parser such as Expat or
xmlproc.  To create an XMLReader for an existing parser, use the
xml.sax.make_parser() function. For example:

    from xml.sax import make_parser

    p = make_parser()      # Create default parser

Without any arguments, the make_parser() function consults an internal
list of parsers and chooses the first one that is installed.  If its
necessary to use a specific parser for some reason, an optional
argument specifying the precise parsing module can also be supplied.
For example:

    p = make_parser("xml.sax.drivers2.drv_xmlproc")    # Create xmlproc parser
    p = make_parser("xml.sax.drivers2.drv_expat")      # Create expat parser

Regardless of the underlying parsing engine, all XMLReader objects support
a common set of methods. 

p.parse(source)

Starts processing SAX events for input source.  source is often an open
file object, but it can also be a filename, a URL, or an InputSource
object (described shortly).  For example:

   p.parse("file.xml")                  # Filename 
   p.parse(open("file.xml"))            # Open file
   p.parse("http://dead.com/file.xml")  # URL

parse() does not return until it has processed the entire input stream.
Therefore, before calling parse(), you need to set up all of the handlers
that will be used to process SAX different types of SAX events.  The
following methods are used to do this:

p.setContentHandler(handler)
    Sets the current ContentHandler.   A ContentHandler is used to process
    XML elements and text. 

p.getContentHandler()
    Returns the current ContentHandler.

p.setDTDHandler(handler)
    Sets the current DTDHandler. A DTDHandler is used to process DTD declarations
    needed for parsing (unparsed entities and attributes).

p.getDTDHandler()
    Returns the current DTDHandler.

p.setEntityResolver(handler)
    Set the current EntityResolver.  An EntityResolver is used to handle all
    external entities.

p.getEntityResolver()
    Returns the current EntityResolver.

p.setErrorHandler(handler)
    Set the current ErrorHandler. An ErrorHandler is used to control how
    parsing errors are handled.

p.getErrorHandler()
    Get the current ErrorHandler.

If a particular handler isn't defined, those SAX events are usually
ignored.  For instance, if you don't define a ContentHandler, the
parser won't do much of anything.

In addition to setting handlers, XMLReaders provide an interface
for enabling or disabling certain XML features.  This is controlled
by the following pair of functions:

p.getFeature(featurename)
    Returns the current setting for parsing feature featurename.

p.setFeature(featurename,value)
    Change the value of featurename to value. 

featurename is usually set to one of the following constants defined in
the xml.sax.handler module.  value is set to 1 or 0 to indicate a true
or false value.  The default values depend on the underlying
parsing engine being used.

feature_namespaces
     Perform namespace processing. 

feature_namespace_prefixes
     Use the original prefixed names and attributes in namespace 
     declarations. Disabled by default.

feature_string_interning
     Intern element names, prefixes, attribute names, namespace URIs,
     and local names using the built-in intern() function.  Disabled
     by default.

feature_validation
     Report all validation errors.

feature_external_ges
     Include all external general entities.

feature_external_pes
     Include all external parameter entities including those
     defined in an external DTD.

For example:

     from xml.sax import handler
     p = make_parser()
     p.setFeature(handler.feature_namespaces,1)
     p.setFeature(handler.feature_validation,1)
   
Most features are read-only during parsing.  Therefore,
setFeature() is almost always used before calling
p.parse().

In addition, XMLReaders have a number of properties that are used
to control behavior of the parser.  Properies are controlled by
the following methods:

p.getProperty(propertyname)
    Return the current setting for property propertyname.

p.setProperty(propertyname,value)
    Change the value of property propertyname to value.

Values for propertyname are also defined in xml.sax.handler.

property_lexical_handler
    Optional extension handler for processing lexical
    events such as comments.  

property_declaration_handler
    Optional extension handler for processing DTD 
    declarations other than those handled by the
    DTDHandler.  

property_dom_node
    DOM iteration. 

property_xml_string
    The literal string of characters that was the
    source for the current event.

As for this writing, none of the above properties appear to be
supported in Python (so this is really only mentioned because it
is part of the SAX API).

Finally, the following method is used to control locale settings
of the parser.

p.setLocale(locale)
    Set the locale for errors and warnings.  This function
    may or may not be supported and it is acceptable for
    SAX parsers to ignore locale requests.  


In an error occurs in any of the SAX functions, one of the
following exceptions will be raised:

SAXException(msg [,exception])
    Base class for all other SAX exceptions.  msg is a
    human readable message and exception optionally
    contains a different exception (this is used to
    encapsulate more specific errors).

SAXParseException(msg, exception, locator)
    Raised when a parsing error occurs.  In addition
    containing an error message, this exception provides
    information about the error location that can
    be obtained using the Locator interface (see
    the ContentHandler section for details).

SAXNotRecognizedException(msg,exception)
    Raised when an XMLReader is asked to support an unrecognized
    feature or property.

SAXNotSupportedException(msg,exception)
    Raised when an XMLReader is asked to enable a feature or
    property that the implementation does not support.



InputSource Objects
-------------------
Internally, all input to a SAX parser is supplied using a special
InputSource object.  InputSource really just defines a common I/O
interface that can be used by a parser.  An instance i of an
InputSource class has the following methods:

i.setPublicId(id)
    Set the public identifier

i.getPublicId()
    Get the public identifier

i.setSystemId(id)
    Set the system identifier

i.getSystemId()  
    Get the system identifier

i.setEncoding(encoding)
    Sets the character encoding.  encoding is typically a string like
    'iso-8859-1' or 'utf-8' that would ordinarily appear in an
    <?xml encoding="..."?> declaration.

i.getEncoding()
    Return the character encoding.

i.setByteStream(bytefile)
    Sets the byte-stream of the object to a Python file-like object.
    The bytefile object should be a raw byte oriented file and not
    a file that performs automatic character conversion (e.g., do not
    pass a file that automatically decodes Unicode characters). 

i.getByteStream()
    Returns the byte-stream for the input source.

i.setCharacterStream(charfile)
    Sets the character stream.  charfile must be a file-like that
    knows how to decode bytes into characters according to the
    inputs encoding scheme.  Usually this is a Unicode file object
    as might be created by the codecs module.

i.getCharacterStream()
    Return the character stream.

If an application wants to supply XML data through a non-traditional
mechanism, the InputSource class can be specialized and modified as
needed.   The modified input can then be passed to the parse() method.
For example:

   from xml.sax import xmlreader
   from xml.sax import make_parser
   class MyInput(xmlreader.InputSource):
        ...
        # specialize InputSource
        ...

   p = make_parser()
   ...
   i = MyInput("foo.xml")        # Create InputSource object
   p.parse(i)                    # Parse

Content Handlers
----------------
Most of the work in a SAX parser is performed by a special
ContentHandler object.  The module xml.sax.handler defines a class
ContentHandler that defines the interface.  However, you often create
a handler by subclassing a default implementation. For example:

from xml.sax import saxutils
# Define a simple SAX handler
class SimpleHandler(saxutils.DefaultHandler):
    def startElement(self,name,attrs):
        print 'Start: ',name,attrs
    def endElement(self,name):
        print 'End: ',name
    def characters(self,data):
        print 'Data: ', repr(data)

sh = SimpleHandler()
p = make_parser()
p.setContentHandler(sh)
p.parse(file)

The following methods of a ContentHandler are invoked during
parsing.

c.setDocumentLocator(locator)

This method provides the parser with an object that can be used 
for location tracking.    This is especially useful for reporting
error messages and knowing where different SAX events occur.
locator is a special Locator object that provides the following
methods:

     locator.getColumnNumber()
     locator.getLineNumber()
     locator.getPublicId()
     locator.getSystemId()

The getColumnNumber() and getLineNumber() methods return the 
approximate location with in a file.  The getPublicID() returns
the public name of an entity (if available).  The getSystemID()
returns the system name being used. This is often a filename
or URL. 

Here is a very simple example of how these functions might be used

class SimpleHandler(saxutils.DefaultHandler):
    # Obtain a locator object
    def setDocumentLocator(self,locator):
        self.locator = locator

    def startElement(self,name,attrs):
        col = self.locator.getColumnNumber()
        line = self.locator.getLineNumber()
        pubid = self.locator.getPublicId()
	sysid = self.locator.getSystemId()
        print 'startElement (%d,%d,%s,%s): %s' % (line,col,pubid,sysid,name)
        
    def endElement(self,name):
        col = self.locator.getColumnNumber()
        line = self.locator.getLineNumber()
        pubid = self.locator.getPublicId()
	sysid = self.locator.getSystemId()
        print 'endElement (%d,%d,%s,%s): %s' % (line,col,pubid,sysid,name)

    def characters(self,data):
        print 'characters: ', repr(data)


Here is a little sample output to see what kind of values are returned:

startElement (16,0,None,guac.xml):  recipe 
characters:  u'\n'
characters:  u'   '
startElement (17,3,None,guac.xml):  title 
characters:  u' '
characters:  u'\n'

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
国产最新精品免费| 欧美网站大全在线观看| 91电影在线观看| 91精品福利在线一区二区三区 | 亚洲精品va在线观看| 蜜桃av一区二区三区| 色94色欧美sute亚洲线路一久 | 欧美色电影在线| 中文天堂在线一区| 看电视剧不卡顿的网站| 欧美午夜精品一区二区蜜桃| 国产午夜亚洲精品午夜鲁丝片| 午夜精品福利一区二区蜜股av | 久久综合九色综合久久久精品综合 | 日韩一区二区视频| 亚洲国产成人porn| 色综合久久天天| 国产精品美女久久久久久久久久久| 裸体歌舞表演一区二区| 欧美日韩国产影片| 一区二区三区中文字幕精品精品| 成人午夜精品一区二区三区| 欧美电影免费提供在线观看| 男男成人高潮片免费网站| 欧美女孩性生活视频| 亚洲国产日日夜夜| 99久久精品国产一区| 日本一区二区动态图| 国产91精品在线观看| 久久九九久精品国产免费直播| 美女被吸乳得到大胸91| 日韩天堂在线观看| 久久精品国产精品青草| 精品91自产拍在线观看一区| 久久aⅴ国产欧美74aaa| 日韩一级二级三级精品视频| 日本vs亚洲vs韩国一区三区| 日韩欧美一级特黄在线播放| 久久er99精品| 国产日韩精品视频一区| 北条麻妃国产九九精品视频| 国产精品不卡在线| 色哟哟精品一区| 亚洲高清三级视频| 日韩欧美在线观看一区二区三区| 麻豆精品视频在线观看| 久久看人人爽人人| 91小视频在线免费看| 亚洲成人自拍偷拍| 精品久久久久久久久久久久包黑料| 九九精品视频在线看| 国产视频一区不卡| 色欧美日韩亚洲| 日本成人在线一区| 久久久久国产精品厨房| 一本在线高清不卡dvd| 亚洲午夜激情av| 26uuu精品一区二区| 成人精品免费网站| 亚洲电影一级黄| 久久日韩精品一区二区五区| av网站一区二区三区| 亚洲444eee在线观看| 欧美激情在线一区二区三区| 色综合天天性综合| 日本亚洲最大的色成网站www| 久久久久久9999| 欧美伊人久久大香线蕉综合69| 视频一区视频二区中文| 中文字幕国产精品一区二区| 欧美日韩国产免费一区二区 | 91久久精品一区二区三区| 日韩和的一区二区| 国产精品全国免费观看高清| 欧美久久久久久久久久| av在线一区二区| 麻豆成人久久精品二区三区小说| 亚洲欧洲精品成人久久奇米网| 欧美日韩中文字幕一区| 国产·精品毛片| 美女一区二区在线观看| 亚洲黄色在线视频| 久久精品一区蜜桃臀影院| 91精品国产综合久久精品| 97aⅴ精品视频一二三区| 国产一区二区三区免费观看| 日韩成人av影视| 亚洲欧美日韩中文播放| 国产日韩影视精品| 精品电影一区二区| 337p亚洲精品色噜噜噜| 91久久一区二区| 成人激情免费视频| 精品亚洲aⅴ乱码一区二区三区| 一区二区三区国产豹纹内裤在线| 国产日本欧洲亚洲| 精品国产凹凸成av人网站| 8x8x8国产精品| 欧美专区亚洲专区| 色悠久久久久综合欧美99| 国产 日韩 欧美大片| 国产一区二区不卡老阿姨| 免费不卡在线观看| 午夜一区二区三区视频| 亚洲一区在线电影| 一区二区三区四区视频精品免费| 久久精品一级爱片| 久久毛片高清国产| 久久综合久久鬼色| 337p日本欧洲亚洲大胆精品| 欧美一区二区三区四区久久| 6080午夜不卡| 日韩三级免费观看| 日韩精品一区二区三区中文不卡| 欧美高清视频不卡网| 91精品免费在线观看| 91精品在线免费| 日韩精品一区二区三区在线播放| 日韩午夜在线观看视频| 精品国产1区二区| 久久影院午夜论| 欧美国产日韩亚洲一区| 日韩一区中文字幕| 亚洲综合自拍偷拍| 亚洲国产视频a| 蜜臀国产一区二区三区在线播放| 麻豆极品一区二区三区| 国产一区二区三区| 成人v精品蜜桃久久一区| www.在线欧美| 欧美自拍丝袜亚洲| 91麻豆精品国产91久久久久久 | 欧美一区二区三区小说| 精品国内片67194| 亚洲国产精品v| 一区二区三区高清在线| 男女激情视频一区| 豆国产96在线|亚洲| 色婷婷国产精品综合在线观看| 欧美日韩一区成人| 精品国精品国产| 自拍偷自拍亚洲精品播放| 亚洲成av人片一区二区梦乃| 久久国产成人午夜av影院| www.日韩大片| 欧美一级二级三级蜜桃| 国产欧美一区二区精品秋霞影院| 亚洲欧美二区三区| 久久精品99国产国产精| 91一区二区三区在线观看| 欧美日韩国产另类一区| 久久综合色8888| 亚洲狠狠爱一区二区三区| 麻豆一区二区在线| 色综合久久88色综合天天| 在线综合+亚洲+欧美中文字幕| 国产欧美日韩三级| 免费精品视频在线| 色综合久久久网| 精品国产乱码久久久久久图片 | 国产露脸91国语对白| 91福利社在线观看| 精品伦理精品一区| 亚洲精品videosex极品| 国产一区二区三区视频在线播放| 色婷婷狠狠综合| 欧美国产97人人爽人人喊| 蜜桃视频一区二区三区| 欧美中文字幕不卡| 国产欧美日韩中文久久| 美女被吸乳得到大胸91| 欧美四级电影在线观看| 亚洲视频在线观看一区| 国产很黄免费观看久久| 日韩三级精品电影久久久| 亚洲一级二级三级| 色综合久久九月婷婷色综合| 欧美国产日韩在线观看| 国产一区 二区 三区一级| 欧美一区二区美女| 亚洲第一久久影院| 色婷婷激情久久| 亚洲欧洲日韩综合一区二区| 国产大片一区二区| 欧美精品一区二区三区高清aⅴ | 国产亚洲午夜高清国产拍精品| 日本麻豆一区二区三区视频| 欧美性高清videossexo| 一区二区在线观看视频| 99v久久综合狠狠综合久久| 国产精品视频在线看| 国产精品一级在线| 精品国产乱码久久久久久蜜臀| 日韩成人午夜电影| 日韩欧美电影在线| 久久精品国内一区二区三区| 6080日韩午夜伦伦午夜伦| 日韩激情av在线| 日韩视频免费观看高清完整版在线观看| 亚洲第一二三四区|