Re: [xml] Building a DOM tree in python using libxml2



Colin Fox said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Daniel Veillard wrote:
| On Thu, Jul 15, 2004 at 12:16:25PM +0800, William M. Brack wrote:
|
|>Colin - Sorry not to have replied to your original post, but I had
|>the same problem as you with not having any examples of a simple
|>Python program that worked :-).  John's example solves that
problem.
|> I'll try to check into why your previous attempt produced a
|>segfault - we can argue over whether or not the code is "correct",
|>but I think we will agree it shouldn't crash in that manner.
|>
|>In fact, I simplified your original post just a bit:-
|>
|>import libxml2
|>d = libxml2.xmlDoc('1.0')
|>print d
|>
|>and got a segfault at that point :-\.  Anyway, I'll try to get it
|>fixed.
|
|
|   Here is the associated code. It's generated code. the xmlDoc and
other
| classes are wrappers for a libxml2 object and must be created with
such
| an object. As pointed by the working example you must use a
library
| call to create those objects.
|
| class xmlDoc(xmlNode):
|     def __init__(self, _obj=None):
|         self._o = _obj
|         xmlNode.__init__(self, _obj=_obj)
|
|   xmlNode.__init__ could check for the type of the object an raise
an
| exception, that would be one way to catch those kind of mistakes.
|
| Daniel
|

It'd be nice actually if _obj was None that the appropriate library
call
got made to manufacture it.

cf

I'm not quite convinced of that - and I'm not certain, but it looks
as if you have a small misunderstanding of the Python bindings.  As
mentioned in the docs http://xmlsoft.org/python.html, "most of the
bindings are automatically generated, and the xml prefix is removed
and the casing convention are kept".  In other words, libxml2.xmlDoc
is *not* the proper way to create a document, and libxml2.xmlNode is
*not* for creating a node in the tree.  They are, in fact, *classes*
to implement the various methods (the proper methods are
libxml2.newDoc and the newChild method of the resulting document
object, as mentioned in John Fleck's email).

I'm still looking at how to cleanly check what object the user
provides (in order to prevent a segfault), but I work slowly when a
lot of stuff I never knew about before is involved :-).

Bill




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]