[Expat-discuss] Beginner Question

Dan Rosen dr at netscape.com
Fri Aug 15 12:51:26 EDT 2003


Hi Steve,

I see what you're asking. The general question here is, how do you get 
at the content (rather than attributes) of a specific element?

First of all, remember that the parser looks at all the data 
sequentially, so all your callbacks will be invoked sequentially. Given 
a document containing just:

   <foo>bar</foo>

... you'll get three callbacks: one for the the start of the "foo" 
element, one for the "bar" character data, and one for the end of the 
"foo" element. Point being, when the "foo" start-element handler is 
invoked, the "bar" text hasn't been seen yet. To put it simply, you need 
to register a separate character data handler (see 
XML_SetCharacterDataHandler, etc.).

Secondly, if you want to handle text data specific to a given element, 
your callback code has to contain some state -- otherwise, when you're 
in the context of a character data handler, you have no idea what 
element the character data belongs to. One possible implementation would 
be that you maintain a stack of tag names: in every start-element 
handler, you push your current tag name to the stack, and in every 
end-element handler, you pop from the stack. This way, your character 
data handler can examine the top element of the stack to determine what 
it should do with the text.

That's one possible implementation ... I can think of other, more 
object-oriented ways of maintaining parse state, but this will do in a 
pinch. I think the fundamental thing you were missing was just a bit of 
the parsing model -- given that knowledge, you can implement this 
however is most appropriate.

Hope this helps,
dr

Steve Messer wrote:

> Pants and price are just attributes of name. But what if you have
> something like this the following:
>  
> <head>
>     <locale>en_US</locale>
>     <form>MEDIUM</form>
>     <ut>F</ut>
>     <ud>mi</ud>
>     <us>mph</us>
>     <up>in</up>
>     <ur>in</ur>
>   </head>
>  
> While parsing I know when I have found the element ut for example.
>  
> Having found the tag <ut> in the 
> startElement(void *userData, const char *name, const char **atts)
> function how do I get the value F?




More information about the Expat-discuss mailing list