aspose file tools*
The moose likes XML and Related Technologies and the fly likes StaX: Transformer.transform does next() automatically, how to controll it? 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 "StaX: Transformer.transform does next() automatically, how to controll it?" Watch "StaX: Transformer.transform does next() automatically, how to controll it?" New topic
Author

StaX: Transformer.transform does next() automatically, how to controll it?

Ratna Lein
Greenhorn

Joined: Jun 22, 2014
Posts: 1
I am using XMLStreamReader to achieve my goal(splitting xml file). It looks good, but still does not give the desired result. My aim is to split every node "nextTag" from an input file:


The outcome should look like this:


Referring to http://stackoverflow.com/questions/5169978/split-1gb-xml-file-using-java I achieved my goal with this code:


Actually very simple. But, my input file is in form from a single line:


My Java code does not produce the desired output anymore, instead just this result:

After spending hours, I am pretty sure to already find out the reason:


It is because, after the transform method being executed, the cursor will automatically moved forward to the next event. And in the code, I have this fraction:


After the first transform, the streamReader gets directly 2 times next():

1. from the transform method
2. from the next method in the while loop

So, in case of this specific line XML, the cursor can never achive the second open tag <nextTag>.
In opposite, if the input XML has a pretty print form, the second <nextTag> can be reached from the cursor because there is a space-event after the first closing tag </nextTag>

Unfortunately, I could not find anything how to do settings, so that the transformator does not automatically spring to next event after performing the transform method. This is so frustating.

Does anybody have any idea how I can deal with it? Also semantically is very welcome. Thank you so much.

Regards,

Ratna

PS. I can surely write a workaround for this problem(pretty print the xml document before transforming it, but this would mean that the input xml was being modified before, this is not allowed).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18880
    
    8

Ratna Lein wrote:The outcome should look like this:


Surely not. That isn't a valid XML document, it's two of them concatenated together. It isn't even an external parsed entity because it isn't valid to have an XML prolog in the middle of such a thing.

So perhaps you were expecting to receive the prolog as the result of your first call to "next"? The prolog isn't part of the document so you aren't going to get that.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18880
    
    8

And welcome to the Ranch!
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 539
    
    3
Unfortunately, I could not find anything how to do settings, so that the transformator does not automatically spring to next event after performing the transform method.

I think you are asking the wrong class to bend to your need...
Does anybody have any idea how I can deal with it?

Instead, you should ask your own application to accommodate the proper behavior of the Transformer. The cursor api is equipped with all the necessary methods to deal with it...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StaX: Transformer.transform does next() automatically, how to controll it?