This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Streaming Xml and well formedness" Watch "Streaming Xml and well formedness" New topic
Author

Streaming Xml and well formedness

joe black
Ranch Hand

Joined: Dec 03, 2003
Posts: 103
Hello,

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

http://www.ibm.com/developerworks/xml/library/x-tipstx4/

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?

Peter
[ July 09, 2007: Message edited by: joe black ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Streaming Xml and well formedness
 
Similar Threads
Generating xml by using Jdom,need to remove DOCTYPE and root element
Problem in sending SOAP message using SAAJ on web service implemented in .NET
Issue in using the DatabaseServerLoginModule for encrypting the password using MD5
org.springframework.ws.client.WebServiceTransportException: Method Not Allowed [405]
Problem with processing data files of size larger than 350 MB