From: Fabrizio Ammollo (f.ammollo@reitek.com)
Date: Wed Feb 21 2001 - 05:39:22 EST
Hi,
It's my first post to the list, and I'll be quite original :-) in asking a
question about one problem I'm having with libxml2 (2.3.1).
A few months ago I developed a simple process which takes an XML file from an
HTTP server using libghttp and then it parses it using the SAX API; BUT, I
did this using the expat library.
Some days ago I was considering about using libxml2 for doing the same thing,
because both of its much greater functionaly, but before all its validation
capabilities. So between yesterday and today I have made the "porting".
At last, things work as they did using expat, but I had lots of troubles
because of the way the callbacks are treated by libxml2; I explain this.
In my program I already did one parsing, without specifying any callback, to
determine the well-formedness of the data, and then I set my handlers (for
startElement, endElement and characters) and redid the parsing to exctract
the data I need. So I did the same, first doing this:
if (xmlSAXUserParseMemory(&xmlDefaultSAXHandler, (void *) NULL,
response_body, response_len))
{
sprintf(errdesc, "xmlSAXUserParseMemory() error\n");
...
}
...
and then I did this:
saxfuncs.startElement = StartElement;
saxfuncs.endElement = EndElement;
saxfuncs.characters = charElement;
if (xmlSAXUserParseMemory(&saxfuncs, &userdata, response_body,
response_len))
{
sprintf(errdesc, "xmlSAXUserParseMemory() error\n");
...
}
...
But on the first run the program always dumped core, and gdb showed me that
it always died into libxml2's internal startDocument callback: it takes a
void *ctx, which is cast to a xmlParserCtxtPtr, but the xmlParseDocument
calls it passing in place of this argument the user_data argument passed to
the xmlSAXUserParseMemory, which does not represent the parser context, but
user-defined data ! So, there is the dereferencing of a NULL pointer and the
program terminates..
I was able to work-around this problem because I first do the first parsing
by having all SAX handlers set to NULL and then setting my wanted handlers
before the second parsing.
My question is, there is something wrong in the way the xmlParseDocument
calls the startDocument callback, or am I missing something about the right
usage of the APIs ?
I think that the problem only arises by using the internally defined
startDocument function, an externally defined callback function would
correctly receive the originally passed user_data.
Another question: the getLineNumber and getColumnNumber functions need a
pointer to the parser context, so the only way to report them if the
xmlSAXUserParseMemory fails is to use in place of it a wrapper of the
xmlSAXParseMemory and return from it the values returned by the two
functions, right ?
I do not have yet explored the "world" of validation, I'll ask explainations
about it if I have problems.
TIA !
-- Bye, Fabrizio Ammollo."Is this foreplay?" "No, this is Nuke Strike. Foreplay has lousy graphics. Beat me again." -- Duckert, in "Bad Rubber," Albedo #0 (comics) ---- Message from the list xml@rpmfind.net Archived at : http://xmlsoft.org/messages/ to unsubscribe: echo "unsubscribe xml" | mail majordomo@rpmfind.net
This archive was generated by hypermail 2b29 : Wed Feb 21 2001 - 06:43:46 EST