[Expat-discuss] how to handle empty elements

Karl Waclawek karl at waclawek.net
Fri Jun 24 14:56:52 CEST 2005


Oliver Mueller wrote:
> Hi
> 
>  
> 
> I'm using EXPAT on Red Hat Linux to import XML data into a database. I found out, if there is an empty element (<x/> or <x></x>), the character data handler does not get called.
> 
> This causes problems because of not well formed SQL insert statement (missing value).
> 
> I intended to solve the problem as follows:
> 
>  
> 
> (1) set a flag false in the start element handler
> 
> (2) set the flag true in the character data handler
> 
> (3) if the flag is false in the end element handler, do an action
> 
> (4) set the flag false in the end element handler
> 
>  
> 
> Is not calling the "character data handler" normal EXPAT behaviour if an element is empty?
> 
> Has anyone made similar experience and got a better solution than my tinkered one?

Normally, if you handle character data properly you would not
need such a flag. As you probably know, Expat might use multiple character
data call-backs to report one contiguous string of characters, so you
need to accumulate them in a buffer anyway. That means, in the start
element call-back you clear the buffer, in the character data call-backs
you accumulate data, and in the end element call-back you use the buffer.
In this case, detecting an empty element is as simple as checking for
an empty buffer.

Karl


More information about the Expat-discuss mailing list