Reputation: 351
I'm trying to do something which I consider a basic task here.
I'm after a simple and tidy Java solution that can split XML content by element. I don't want to load the whole content in memory (i.e. I want to stream the elements) and I want to get my hands on each element's string representation, in order to send it to wherever I want to.
My obviously unsuccessful pull parsing attempt with StAX:
while(xsr.hasNext()) {
if( xsr.next() == XMLEvent.START_ELEMENT && xsr.getLocalName().equals("Order") ) {
String element = xsr.getElementText();
// or String element = xsr.getText();
}
}
I've also tried Smooks with no success. In particular, the examples provided here http://www.smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Basic_Splitting_and_Routing don't provide a neat way of getting hold of the String XML of the elements. It's all easy and smooth if you want to send the XML elements to file, to a DB or to a JMS queue... but not in case I wanna get my hands on the String XML.
I'm so discouraged that I started thinking that it might be less effort to write my own SAX based solution. Anyone that can prove me wrong?
Upvotes: 0
Views: 583
Reputation: 284
It's hard to tell if Smooks is the right tool or not because you're not describing the wider problem you are trying to solve e.g. where the data fragments need to get to eventually (which does add/take from the value of Smooks) + the size of the data stream.
Anyway, if you wanted to use Smooks, one option might be to capture the XML fragments you are interested in using the DomModelCreator [1] and then serialize those DOM fragments using either a custom "Visitor" impl [2], or a groovy scriptlet [3]. If this is a huge sata stream then you'll also need to handle those fragments on the fly (as after they are serialized and before the next fragment). This could also be done in the same custom visitor impl or groovy script, but that really depends on the user case. In any case, the fragment visitor is where you put the code that will execute on a specific fragment and do "whatever".
[1] http://www.smooks.org/mediawiki/index.php?title=V1.5:Smooks_v1.5_User_Guide#Mixing_DOM_and_SAX
[3] http://www.smooks.org/mediawiki/index.php?title=V1.5:Smooks_v1.5_User_Guide#Scripting
Upvotes: 1