[xml] Re: SAX and userData



On Sun, 21 Apr 2002, David F. Newman wrote:

Hi there,
I hope this is an easy one.  I've been trying to get my userData
in the callbacks but I'm doing something wrong or I just don't
understand how it is supposed to work.  I first start by calling:

xmlSAXParseFileWithData(&handler, xmlfilename, 0, (void *)5);

Now in this example I would think that the userData would be set
to 5.  However, in my startDocument(void *ctxt) callback, ctxt
is not set to 5.  I am assuming that libxml is calling startDocument
with userData due to grepping through the source code.  But at the
same time it looks like xmlSAXParseFileWithData(..., data) sets
ctxt->_private to data, and I was not able to figure out how
_private gets to userData.

Any suggestions?  I'm using 2.4.20.  Thanks alot.


After greping through the libxml source code some more I figured
this out on my own.  It turns out that void *ctx which is passed
to the callbacks is in fact a pointer to the user data and the
parser context at the same time.  I was getting thrown off by
the fact that the call backs were being called with ctxt->userData.
So I assumed that my pointer should've been directly passed to
the callbacks.  However, I found that ctxt->userData was being
set to ctxt in various places so that led me to believe that
the actual parser context was being passed to the call backs.
Therefore, I found my user data, not in userData but in _private.

The page that documents xmlSAXParseFileWithData() describes:
"User data (void *) is stored within the parser context, so it is
available nearly everywhere in libxml."

Maybe it should be updated to include that the user data is
stored in the parser context's _private member and not in userData.

Also the documentation for the callbacks state:
ctx : the user data (XML parser context)

As a beginner to libxml this seemed contradictory.  Which was it?
The user data or the parser context?  Now I know that it could
be either.  Wouldn't it be better to always pass the parser context
and change the prototype to be xmlParserCtxtPtr ctx for the sake
of type safety?

---
David F. Newman
Unix Administrator
dnewman maraudingpirates org





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