mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-07-29 11:41:22 +03:00
fixed to point to releases of libxml2-2.6, Daniel
This commit is contained in:
@ -33,7 +33,7 @@ or libxslt wrappers or bindings:</p><ul><li><a href="http://libxmlplusplus.sourc
|
||||
libxml2</a> with Kylix, Delphi and other Pascal compilers.</li>
|
||||
<li>Uwe Fechner also provides <a href="http://sourceforge.net/projects/idom2-pas/">idom2</a>, a DOM2
|
||||
implementation for Kylix2/D5/D6 from Borland.</li>
|
||||
<li>Wai-Sun "Squidster" Chia provides <a href="http://www.rubycolor.org/arc/redist/">bindings for Ruby</a> and
|
||||
<li>Wai-Sun "Squidster" Chia provides <a href="http://www.rubycolor.org/arc/redist/">bindings for Ruby</a> and
|
||||
libxml2 bindings are also available in Ruby through the <a href="http://libgdome-ruby.berlios.de/">libgdome-ruby</a> module
|
||||
maintained by Tobias Peters.</li>
|
||||
<li>Steve Ball and contributors maintains <a href="http://tclxml.sourceforge.net/">libxml2 and libxslt bindings for
|
||||
@ -58,23 +58,23 @@ build the bindings is python/generator.py in the source distribution.</p><p>To i
|
||||
<li>Otherwise use the <a href="ftp://xmlsoft.org/python/">libxml2-python
|
||||
module distribution</a> corresponding to your installed version of
|
||||
libxml2 and libxslt. Note that to install it you will need both libxml2
|
||||
and libxslt installed and run "python setup.py build install" in the
|
||||
and libxslt installed and run "python setup.py build install" in the
|
||||
module tree.</li>
|
||||
</ul><p>The distribution includes a set of examples and regression tests for the
|
||||
python bindings in the <code>python/tests</code> directory. Here are some
|
||||
excerpts from those tests:</p><h3>tst.py:</h3><p>This is a basic test of the file interface and DOM navigation:</p><pre>import libxml2, sys
|
||||
|
||||
doc = libxml2.parseFile("tst.xml")
|
||||
if doc.name != "tst.xml":
|
||||
print "doc.name failed"
|
||||
doc = libxml2.parseFile("tst.xml")
|
||||
if doc.name != "tst.xml":
|
||||
print "doc.name failed"
|
||||
sys.exit(1)
|
||||
root = doc.children
|
||||
if root.name != "doc":
|
||||
print "root.name failed"
|
||||
if root.name != "doc":
|
||||
print "root.name failed"
|
||||
sys.exit(1)
|
||||
child = root.children
|
||||
if child.name != "foo":
|
||||
print "child.name failed"
|
||||
if child.name != "foo":
|
||||
print "child.name failed"
|
||||
sys.exit(1)
|
||||
doc.freeDoc()</pre><p>The Python module is called libxml2; parseFile is the equivalent of
|
||||
xmlParseFile (most of the bindings are automatically generated, and the xml
|
||||
@ -101,14 +101,14 @@ def noerr(ctx, str):
|
||||
|
||||
libxml2.registerErrorHandler(noerr, None)
|
||||
|
||||
ctxt = libxml2.createFileParserCtxt("invalid.xml")
|
||||
ctxt = libxml2.createFileParserCtxt("invalid.xml")
|
||||
ctxt.validate(1)
|
||||
ctxt.parseDocument()
|
||||
doc = ctxt.doc()
|
||||
valid = ctxt.isValid()
|
||||
doc.freeDoc()
|
||||
if valid != 0:
|
||||
print "validity check failed"</pre><p>The first thing to notice is the call to registerErrorHandler(), it
|
||||
print "validity check failed"</pre><p>The first thing to notice is the call to registerErrorHandler(), it
|
||||
defines a new error handler global to the library. It is used to avoid seeing
|
||||
the error messages when trying to validate the invalid document.</p><p>The main interest of that test is the creation of a parser context with
|
||||
createFileParserCtxt() and how the behaviour can be changed before calling
|
||||
@ -118,8 +118,8 @@ C function interfaces in terms of objects method as much as possible. The
|
||||
best to get a complete view of what methods are supported is to look at the
|
||||
libxml2.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 libxml2
|
||||
|
||||
ctxt = libxml2.createPushParser(None, "<foo", 4, "test.xml")
|
||||
ctxt.parseChunk("/>", 2, 1)
|
||||
ctxt = libxml2.createPushParser(None, "<foo", 4, "test.xml")
|
||||
ctxt.parseChunk("/>", 2, 1)
|
||||
doc = ctxt.doc()
|
||||
|
||||
doc.freeDoc()</pre><p>The context is created with a special call based on the
|
||||
@ -129,71 +129,71 @@ the resource in case URI-References need to be computed by the parser.</p><p>The
|
||||
setting 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 case
|
||||
the parser does not build a document, but provides callback information as
|
||||
the parser makes progresses analyzing the data being provided:</p><pre>import libxml2
|
||||
log = ""
|
||||
log = ""
|
||||
|
||||
class callback:
|
||||
def startDocument(self):
|
||||
global log
|
||||
log = log + "startDocument:"
|
||||
log = log + "startDocument:"
|
||||
|
||||
def endDocument(self):
|
||||
global log
|
||||
log = log + "endDocument:"
|
||||
log = log + "endDocument:"
|
||||
|
||||
def startElement(self, tag, attrs):
|
||||
global log
|
||||
log = log + "startElement %s %s:" % (tag, attrs)
|
||||
log = log + "startElement %s %s:" % (tag, attrs)
|
||||
|
||||
def endElement(self, tag):
|
||||
global log
|
||||
log = log + "endElement %s:" % (tag)
|
||||
log = log + "endElement %s:" % (tag)
|
||||
|
||||
def characters(self, data):
|
||||
global log
|
||||
log = log + "characters: %s:" % (data)
|
||||
log = log + "characters: %s:" % (data)
|
||||
|
||||
def warning(self, msg):
|
||||
global log
|
||||
log = log + "warning: %s:" % (msg)
|
||||
log = log + "warning: %s:" % (msg)
|
||||
|
||||
def error(self, msg):
|
||||
global log
|
||||
log = log + "error: %s:" % (msg)
|
||||
log = log + "error: %s:" % (msg)
|
||||
|
||||
def fatalError(self, msg):
|
||||
global log
|
||||
log = log + "fatalError: %s:" % (msg)
|
||||
log = log + "fatalError: %s:" % (msg)
|
||||
|
||||
handler = callback()
|
||||
|
||||
ctxt = libxml2.createPushParser(handler, "<foo", 4, "test.xml")
|
||||
chunk = " url='tst'>b"
|
||||
ctxt = libxml2.createPushParser(handler, "<foo", 4, "test.xml")
|
||||
chunk = " url='tst'>b"
|
||||
ctxt.parseChunk(chunk, len(chunk), 0)
|
||||
chunk = "ar</foo>"
|
||||
chunk = "ar</foo>"
|
||||
ctxt.parseChunk(chunk, len(chunk), 1)
|
||||
|
||||
reference = "startDocument:startElement foo {'url': 'tst'}:" + \
|
||||
"characters: bar:endElement foo:endDocument:"
|
||||
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 entry
|
||||
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 entry
|
||||
points which can be called by the parser as it makes progresses to indicate
|
||||
the information set obtained. The full set of callback is larger than what
|
||||
the callback class in that specific example implements (see the SAX
|
||||
definition for a complete list). The wrapper will only call those supplied by
|
||||
the object when activated. The startElement receives the names of the element
|
||||
and a dictionary containing the attributes carried by this element.</p><p>Also note that the reference string generated from the callback shows a
|
||||
single character call even though the string "bar" is passed to the parser
|
||||
single character call even though the string "bar" is passed to the parser
|
||||
from 2 different call to parseChunk()</p><h3>xpath.py:</h3><p>This is a basic test of XPath wrappers support</p><pre>import libxml2
|
||||
|
||||
doc = libxml2.parseFile("tst.xml")
|
||||
doc = libxml2.parseFile("tst.xml")
|
||||
ctxt = doc.xpathNewContext()
|
||||
res = ctxt.xpathEval("//*")
|
||||
res = ctxt.xpathEval("//*")
|
||||
if len(res) != 2:
|
||||
print "xpath query: wrong node set size"
|
||||
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"
|
||||
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 XPath
|
||||
@ -208,12 +208,12 @@ python:</p><pre>import libxml2
|
||||
def foo(ctx, x):
|
||||
return x + 1
|
||||
|
||||
doc = libxml2.parseFile("tst.xml")
|
||||
doc = libxml2.parseFile("tst.xml")
|
||||
ctxt = doc.xpathNewContext()
|
||||
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
|
||||
res = ctxt.xpathEval("foo(1)")
|
||||
libxml2.registerXPathFunction(ctxt._o, "foo", None, foo)
|
||||
res = ctxt.xpathEval("foo(1)")
|
||||
if res != 2:
|
||||
print "xpath extension failure"
|
||||
print "xpath extension failure"
|
||||
doc.freeDoc()
|
||||
ctxt.xpathFreeContext()</pre><p>Note how the extension function is registered with the context (but that
|
||||
part 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 extension
|
||||
@ -232,9 +232,9 @@ evaluation point.</p><h3>Memory debugging:</h3><p>last but not least, all tests
|
||||
libxml2.debugMemory(1)</pre><p>and ends with the following epilogue:</p><pre>#memory debug specific
|
||||
libxml2.cleanupParser()
|
||||
if libxml2.debugMemory(1) == 0:
|
||||
print "OK"
|
||||
print "OK"
|
||||
else:
|
||||
print "Memory leak %d bytes" % (libxml2.debugMemory(1))
|
||||
print "Memory leak %d bytes" % (libxml2.debugMemory(1))
|
||||
libxml2.dumpMemory()</pre><p>Those activate the memory debugging interface of libxml2 where all
|
||||
allocated block in the library are tracked. The prologue then cleans up the
|
||||
library state and checks that all allocated memory has been freed. If not it
|
||||
|
Reference in New Issue
Block a user