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 Streaming Xml and well formedness 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 "Streaming Xml and well formedness" Watch "Streaming Xml and well formedness" New topic

Streaming Xml and well formedness

joe black
Ranch Hand

Joined: Dec 03, 2003
Posts: 103

I am creating a REST style webservice, and I am using jibx xml binding framework to create the response. I configured jibx to use StAX as its parser. I have run into an issue however, and would like to know how other people solve it:

When there is an exception thrown during the binding process, the client could get back an incomplete xml response, since its being streamed.
Something like the following:

What I would really like to do is just have an error response returned, ie catch the exception and modify the response. Currently the way I am solving this issue is by having jibx write to a StringWriter, then I spit that back, and if there is an exception I can clear it out and add my error response:

Is there a better way? The above solution fixes the problem, but now I am creating the entire response in memory. How do other people handle this problem? I'd rather not send back invalid xml...

It seems like a choice between being memory intensive for completeness, or being incomplete for better performance

Also keep in mind that this is not an exception in the business logic, and it should never happen.
[ July 09, 2007: Message edited by: joe black ]
joe black
Ranch Hand

Joined: Dec 03, 2003
Posts: 103
Let me elaborate.

The following is an example taken from

Which has been modified to show my point. It uses stax, and when an
error is thrown
while writing to the stream, I can recover gracefully and send back well
formed xml with
an error element.

Notice I am throwing an Exception. This would be similar to a
JibxException being thrown during marshalling.
However, I catch this exception, add an error element as the end
element, then call writeEndDocument which
closes all open structures. Can something similar be done with jibx?
instead of sending back xml that is
not well formed? How does any xml to object binding framework handle this use case?

[ July 09, 2007: Message edited by: joe black ]
I agree. Here's the link:
subject: Streaming Xml and well formedness
It's not a secret anymore!