⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 python.html

📁 libxml,在UNIX/LINUX下非常重要的一个库,为XML相关应用提供方便.目前上载的是最新版本,若要取得最新版本,请参考里面的readme.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
best to get a complete view of what methods are supported is to look at thelibxml2.py module containing all the wrappers.</p><h3>push.py:</h3><p>This test show how to activate the push parser interface:</p><pre>import libxml2ctxt = libxml2.createPushParser(None, "&lt;foo", 4, "test.xml")ctxt.parseChunk("/&gt;", 2, 1)doc = ctxt.doc()doc.freeDoc()</pre><p>The context is created with a special call based on thexmlCreatePushParser() from the C library. The first argument is an optionalSAX callback object, then the initial set of data, the length and the name ofthe resource in case URI-References need to be computed by the parser.</p><p>Then the data are pushed using the parseChunk() method, the last callsetting the third argument terminate to 1.</p><h3>pushSAX.py:</h3><p>this test show the use of the event based parsing interfaces. In this casethe parser does not build a document, but provides callback information asthe parser makes progresses analyzing the data being provided:</p><pre>import libxml2log = ""class callback:    def startDocument(self):        global log        log = log + "startDocument:"    def endDocument(self):        global log        log = log + "endDocument:"    def startElement(self, tag, attrs):        global log        log = log + "startElement %s %s:" % (tag, attrs)    def endElement(self, tag):        global log        log = log + "endElement %s:" % (tag)    def characters(self, data):        global log        log = log + "characters: %s:" % (data)    def warning(self, msg):        global log        log = log + "warning: %s:" % (msg)    def error(self, msg):        global log        log = log + "error: %s:" % (msg)    def fatalError(self, msg):        global log        log = log + "fatalError: %s:" % (msg)handler = callback()ctxt = libxml2.createPushParser(handler, "&lt;foo", 4, "test.xml")chunk = " url='tst'&gt;b"ctxt.parseChunk(chunk, len(chunk), 0)chunk = "ar&lt;/foo&gt;"ctxt.parseChunk(chunk, len(chunk), 1)reference = "startDocument:startElement foo {'url': 'tst'}:" + \             "characters: bar:endElement foo:endDocument:"if log != reference:    print "Error got: %s" % log    print "Expected: %s" % reference</pre><p>The key object in that test is the handler, it provides a number of entrypoints which can be called by the parser as it makes progresses to indicatethe information set obtained. The full set of callback is larger than whatthe callback class in that specific example implements (see the SAXdefinition for a complete list). The wrapper will only call those supplied bythe object when activated. The startElement receives the names of the elementand a dictionary containing the attributes carried by this element.</p><p>Also note that the reference string generated from the callback shows asingle character call even though the string "bar" is passed to the parserfrom 2 different call to parseChunk()</p><h3>xpath.py:</h3><p>This is a basic test of XPath wrappers support</p><pre>import libxml2doc = libxml2.parseFile("tst.xml")ctxt = doc.xpathNewContext()res = ctxt.xpathEval("//*")if len(res) != 2:    print "xpath query: wrong node set size"    sys.exit(1)if res[0].name != "doc" or res[1].name != "foo":    print "xpath query: wrong node set value"    sys.exit(1)doc.freeDoc()ctxt.xpathFreeContext()</pre><p>This test parses a file, then create an XPath context to evaluate XPathexpression on it. The xpathEval() method execute an XPath query and returnsthe result mapped in a Python way. String and numbers are natively converted,and node sets are returned as a tuple of libxml2 Python nodes wrappers. Likethe document, the XPath context need to be freed explicitly, also not thatthe result of the XPath query may point back to the document tree and hencethe document must be freed after the result of the query is used.</p><h3>xpathext.py:</h3><p>This test shows how to extend the XPath engine with functions written inpython:</p><pre>import libxml2def foo(ctx, x):    return x + 1doc = libxml2.parseFile("tst.xml")ctxt = doc.xpathNewContext()libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)res = ctxt.xpathEval("foo(1)")if res != 2:    print "xpath extension failure"doc.freeDoc()ctxt.xpathFreeContext()</pre><p>Note how the extension function is registered with the context (but thatpart is not yet finalized, this may change slightly in the future).</p><h3>tstxpath.py:</h3><p>This test is similar to the previous one but shows how the extensionfunction can access the XPath evaluation context:</p><pre>def foo(ctx, x):    global called    #    # test that access to the XPath evaluation contexts    #    pctxt = libxml2.xpathParserContext(_obj=ctx)    ctxt = pctxt.context()    called = ctxt.function()    return x + 1</pre><p>All the interfaces around the XPath parser(or rather evaluation) contextare not finalized, but it should be sufficient to do contextual work at theevaluation point.</p><h3>Memory debugging:</h3><p>last but not least, all tests starts with the following prologue:</p><pre>#memory debug specificlibxml2.debugMemory(1)</pre><p>and ends with the following epilogue:</p><pre>#memory debug specificlibxml2.cleanupParser()if libxml2.debugMemory(1) == 0:    print "OK"else:    print "Memory leak %d bytes" % (libxml2.debugMemory(1))    libxml2.dumpMemory()</pre><p>Those activate the memory debugging interface of libxml2 where allallocated block in the library are tracked. The prologue then cleans up thelibrary state and checks that all allocated memory has been freed. If not itcalls dumpMemory() which saves that list in a <code>.memdump</code> file.</p><p><a href="bugs.html">Daniel Veillard</a></p></td></tr></table></td></tr></table></td></tr></table></td></tr></table></td></tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -