File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes characters() callback for empty elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "characters() callback for empty elements" Watch "characters() callback for empty elements" New topic

characters() callback for empty elements

Alagu Seenivasan

Joined: Sep 30, 2000
Posts: 18
Anybody know a SAX implementing parser which fires "character()" callback even for empty elements ?
Thank you,
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
I don't know of any, and it seems a very strange requirement. Remember that in XML, <a></a> is treated exactly the same as <a/>. Would you want an empty character string in both cases?
If I need to know whether any characters were supplied, I set a flag if any characters are supplied, and check it when I am notified of the end tag.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Alagu Seenivasan

Joined: Sep 30, 2000
Posts: 18
This indeed is a strange requirement because I need to convert the xml into a fixed length text file where,if the element is not present there should be so many blanks. So after looking around I had already done exactly what you said.
In the "startelement" set a flag and remember the supposed length
and reset the flag in the "characters". check the flag in the "endelement" and do accordingly.
I know that the firing "characters" call back is (parser) implementation dependent.
My problem is solved for the time being. If I come across a better solution, I will post it here.
Thanks Frank.
[This message has been edited by Alagu Seenivasan (edited October 02, 2000).]
Sarada Bhasker
Ranch Hand

Joined: Sep 11, 2000
Posts: 94
Hi Alagu,
I would like to know how you solved the problem.
Alagu Seenivasan

Joined: Sep 30, 2000
Posts: 18
Assumption: characters() callback is not automatically fired for empty elements.
I agree. Here's the link:
subject: characters() callback for empty elements
It's not a secret anymore!