Re: checking DTD when writing XML
On Tue, 15 Sep 2009, Albert wrote:
Tom Anderson a ?crit :
On Mon, 14 Sep 2009, Albert wrote:
Hi, is there any library out there which checks XML when writing it ? I've
tried googling, but without any result.
Nothing springs to mind.
You could send your output to a validating document builder, and if it all
goes in successfully, you know it's okay. The key code is in here:
http://www.herongyang.com/XML/DTD-Validation-of-XML-with-DTD-Using-DOM.html
However, this would involve building a DOM tree that you didn't need.
DOM is a no go for me, in last resort i could re-parse the output file
(with DTD validation) when the writing is done, but i'd like it to be
done on the fly...
Thinking about it, you could do this without actually building a DOM tree.
You use SAX validation, like this:
http://www.ibm.com/developerworks/library/x-tipeh.html
And you need to convert your output stream into an input stream. This is
easily done with a PipedReader/PipedWriter. You then have the parser
running in a separate thread, and have a mechanism for signalling errors
back to the writer (a volatile variable would do it).
It would be easy to do this if you didn't need precise exceptions on the
writer side - exceptions thrown from the call that wrote the offending
text. I think it would be possible to do precise exceptions, but it would
take more attention to detail.
Since you actually need some output from this contraption, you either have
to tee the output stream (easy - and there's a stream in Apache commons-io
to do it) or have the parser re-write what it reads to the final output.
tom
--
As a matter of fact, it is estimated that 10% of all meth labs explode.