java - StaX parsing: Transformer.transform method moves cursor automatically, not always nice -



java - StaX parsing: Transformer.transform method moves cursor automatically, not always nice -

i using xmlstreamreader accomplish goal(splitting xml file). looks good, still not give desired result. aim split every node "nexttag" input file:

<?xml version="1.0" encoding="utf-8"?> <firsttag> <nexttag>1</nexttag> <nexttag>2</nexttag> </firsttag>

the outcome should this:

<?xml version="1.0" encoding="utf-8"?><nexttag>1</nexttag> <?xml version="1.0" encoding="utf-8"?><nexttag>2</nexttag>

referring split 1gb xml file using java achieved goal code:

import java.io.bufferedreader; import java.io.fileinputstream; import java.io.inputstream; import java.io.inputstreamreader; import java.io.stringwriter; import javax.xml.stream.xmlinputfactory; import javax.xml.stream.xmlstreamreader; import javax.xml.transform.transformer; import javax.xml.transform.transformerfactory; import javax.xml.transform.stax.staxsource; import javax.xml.transform.stream.streamresult; public class demo4 { public static void main(string[] args) throws exception { inputstream inputstream = new fileinputstream("input.xml"); bufferedreader in = new bufferedreader(new inputstreamreader(inputstream)); xmlinputfactory mill = xmlinputfactory.newinstance(); transformerfactory tf = transformerfactory.newinstance(); transformer t = tf.newtransformer(); xmlstreamreader streamreader = factory.createxmlstreamreader(in); while (streamreader.hasnext()) { streamreader.next(); if (streamreader.geteventtype() == xmlstreamreader.start_element && "nexttag".equals(streamreader.getlocalname())) { stringwriter author = new stringwriter(); t.transform(new staxsource(streamreader), new streamresult( writer)); string output = writer.tostring(); system.out.println(output); } } } }

actually simple. but, input file in form single line:

<?xml version="1.0" encoding="utf-8"?><firsttag><nexttag>1</nexttag><nexttag>2</nexttag></firsttag>

my java code not produce desired output anymore, instead result:

<?xml version="1.0" encoding="utf-8"?><nexttag>1</nexttag>

after spending hours, pretty sure find out reason:

t.transform(new staxsource(streamreader), new streamresult(writer));

it because, after transform method beingness executed, cursor automatically moved forwards next event. , in code, have fraction:

while (streamreader.hasnext()) { streamreader.next(); ... t.transform(new staxsource(streamreader), new streamresult(writer)); ... }

after first transform, streamreader gets straight 2 times next():

1. transform method 2. next method in while loop

so, in case of specific line xml, cursor can never achive sec open tag . in opposite, if input xml has pretty print form, sec can reached cursor because there space-event after first closing tag

unfortunately, not find how settings, transformator not automatically spring next event after performing transform method. frustating.

does have thought how can deal it? semantically welcome. give thanks much.

regards,

ratna

ps. can certainly write workaround problem(pretty print xml document before transforming it, mean input xml beingness modified before, not allowed)

as elaborated did transformation step proceed next create element if element-nodes follow straight each other.

in order deal this, can rewrite code using nested while loops, this:

while(reader.next() != xmlstreamconstants.end_document) { while(reader.geteventtype() == xmlstreamconstants.start_element && reader.getlocalname().equals("nexttag")) { stringwriter author = new stringwriter(); // transform current node string, moves cursor next start_element t.transform(new staxsource(reader), new streamresult(writer)); system.out.println(writer.tostring()); } }

java xml xml-parsing sax stax

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -