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

Nick MacDonald nickmacd at gmail.com
Tue Aug 5 07:31:45 CEST 2008


Yuki:

Based on the code you sent, I don't think you're doing it quite
correctly and while it might happen to work, its not guaranteed to
keep working.

You need to detect the tags in the start tag function, not in the
element text function.  The element text function doesn't happen to
terminate the whole body of the document, which is why I think your
code happens to work... by accident...

BTW, that XML file is not very well designed... what you really want
is something better, like this:
<Dept name="Administration">
  <DeptHead>Bob Smith</DeptHead>
  <Employees>
    <Employee>Jane Jones</Employee>
    <Employee>John Doe</Employee>
  </Employees>
</Dept>

But I guess that's a matter of personal taste...

Based on that file, what you really want it something like this pseudo-code:

StartTag Callback:
If (0 == strcmp(tagPassedIntoCallback, "DeptHead"))
{
  // this is the tag we're interested in...
  CurrentTagIsHead =true;
}
else
{
  CurrentTagIsHead=false;
  if (0 == strcmp(tagPassedIntoCallback, "Dept"))
  {
    currentDeptName=findNameParameter(numparametersPassedToCallback,
ParameterListPassedToCallback);
  }
}

Body Text Callback:
if (CurrentTagIsHead)
{
  deptHeadName=makeStringCopyOfTextPassedIntoCallback;
  printf("Head of department %s is %s\n", currentDeptName, deptHeadName);
}


As you can see.. do strstr() totally defeats the purpose of using
eXpat, because now you are trying to parse the XML instead of letting
it do all the work for you.

You need to realize that eXpat is event driven, and that you need to
use global state to track the current state of your parsing in the
file, which is what I implied in the pseudo code above...


Good luck...
  Nick


On Mon, Aug 4, 2008 at 9:44 PM, yuki latt <yuki.latt at gmail.com> wrote:
>      My problem is solved. :)   I found three things.
>
> 1) XML file should not have unnecessary indent between tag.
>
> 2) I have to reallocate the memory to avoid core dumped.
>
> 3)  To get rid of recursive calls, I used break for while loop of
> "while(strstr(xmlData,"</element>")!=NULL)".   After searching xmlData until
> end_tag is found no more, while loop stop.
>
>
> Thank you so much for your ideas and kind help. :)


More information about the Expat-discuss mailing list