Making custom transformations for different event types with StAX using EventFilter
and StreamFilter
I feel being forced into a procedural implementation - extract these events and process them, filter those events and process them, than put all the results back together and return.
SAX seems to have a really nice feature there - chainable filters based on XMLFilters.
I would prefer to keep my implementation StAX-based, but to somehow incorporate or emulate the chainable filters from SAX.
Can this be done with a reasonable effort and how? Is there an implementation already that I have missed?
Due to project limitations I have to stick to the JDK, so please do not suggest using a different library as an answer, but feel free to suggest one in the comments - I'd love to know what`s out there.
Take a look at EventReaderDelegate
and StreamReaderDelegate
. These classes will allow you to wrap a parent event or stream reader so that you can interpose whatever logic you'd like.
While you can use delegates, I think one of very few areas where SAX has edge over Stax is ability to construct efficients modular pipelines. Stax is more optimal for recursive-descent approaches, as well as partial data binding (can traverse stream; hand chunks to JAXB, continue, very simple, powerful).
来源:https://stackoverflow.com/questions/5566836/is-there-a-way-to-build-a-stax-filter-chain