Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ratna Lein
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic