Re: [xml] Linx number available via SAX interface?

Date view Thread view Subject view Author view

From: Shane Adams (adamsch1@yahoo.com)
Date: Fri Jul 28 2000 - 14:49:30 EDT


/* Setup parser and callbacks */
strcpy(filename, "testfile.xml");
ctxt = xmlCreateFileParserCtxt(filename);
ctxt->sax = debugSAXHandler; /* struct of func
callbacks - required to do something useful in SAX */
ctxt->userData = my_data; /* See below */
my_data->p_ctxt = ctxt; /* This is the important bit
*/
xmlParseDocument(ctxt); /* Parse the file */

where my_data is defined as

typedef struct _mydata {
  /* Other pointers to your data here */
  void *p_ctxt; /* Hold a refernce to the parser
context */
} mydata;

Now for your error handler: The error handler is one
of the callbacks that you setup for SAX. The real
trick is that the first arg (in the callbacks) is a
pointer to 'your' data. Useful for maintaining state
information. I just cast it to a pointer to a struct
('my' data is a structure). One of the struct fields
is a pointer back to the parsing context. This
context hold state information about the parser, which
contains things like the line number.

void
errorDebug(void *ctx, const char *msg, ...)
{
  xmlParserCtxtPtr ctxt;
  xmlParserInputPtr input;
  mydata *p = (mydata *)ctx;

  ctxt = (xmlParserCtxtPtr) p->p_ctxt;
  input = ctxt->input;

  fprintf(stderr,"Error: %d %s\n", input->line,
          input->filename );

}

--- Dave Doolin <doolin@cs.utk.edu> wrote:
>
>
>
> >
> > I managed to figure it out after looking through
> the
> > library more. I create a FileParserCtxt, then set
> my
> > SAXHandler and set my 'user data'. The 'user
> data'
> > structure contains a pointer back to the
> > FileParserCtxt. When each SAX callback is
> invoked, I
> > receive my own datastructure that now contains a
> > pointer to the original context, and hence has the
> > line number, file name etc.
> >
> > If anyone is interested, I can provide the code
> > example to explain more.
> >
>
> This is for error handling right?
>
> Example code would be great!
>
> Thanks,
>
> Dave D
>
>
>
> ----
> Message from the list xml@xmlsoft.org
> Archived at : http://xmlsoft.org/messages/
> to unsubscribe: echo "unsubscribe xml" | mail
majordomo@xmlsoft.org

__________________________________________________
Do You Yahoo!?
Kick off your party with Yahoo! Invites.
http://invites.yahoo.com/

----
Message from the list xml@xmlsoft.org
Archived at : http://xmlsoft.org/messages/
to unsubscribe: echo "unsubscribe xml" | mail  majordomo@xmlsoft.org


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Wed Aug 02 2000 - 12:30:24 EDT