[Expat-discuss] extracting value other than element and attribute

Nick MacDonald nickmacd at gmail.com
Thu Jul 24 14:02:24 CEST 2008


Well, getting more than one is not a bad thing if you have multiple
tags  ;-)  But I assume you mean you're getting more than one for one
specific tag.  There are a number of ways to handle the problem, which
one you choose has a lot to do with the particular application you
were writing.  Imagine if the was a tag definition for an email and it
had <body></body> tags and had big globs of text in there as the whole
body of long emails would show up inside.  In that case, you would be
building something expecting large amounts of text and would design
accordingly.

However, if you're just getting the occasional second callback for the
same tag, then you can probably get away with some calls to realloc()
to make your memory block larger.

Some basic design principals of event based (SAX) XML parsers:
  - you may need a stack (as in push/pop operations) to know the exact
"path" you're at in the file.
  - you'll probably need to use "user data structures" outside of the
parser to track your state (or use globals, depending on personal
tastes)
  - You can't look forward in the document, so you need to design your
XML handling routines accordingly, and you may even redesign your
input XML to make like easier.

So, in this case... you might have a pointer to the contents of the
current tag, and you set it to NULL in the routine that receives the
start of the tag, and then in the routine that handles the body you
write code along the lines of:
  if (pointer to current tag content is null)
    current tag body size=length of current section of document as
passed to handler
    pointer to current tag content =malloc(current tag body size)
else
  current tag body size += length of current section of document as
passed to handler
    pointer to current tag content = realloc(current tag body size)
end if

Hopefully that make some sense for you... if it does not, I suggest
maybe you to some searching in the list archives on this topic...

Good luck,
  Nick

On Thu, Jul 24, 2008 at 1:32 AM, yuki latt <yuki.latt at gmail.com> wrote:
>           Thank you so much for your reply. As you said, I got more than one
> of that callbacks. Could you please tell me how I can fix this?


More information about the Expat-discuss mailing list