java - Iterate XML input stream with System.out.println() -
i importing xml file inputstream , trying unmarshalling using jaxb. want read out in input stream can better diagnose subsequent steps. how iterate through xml content in inputstream?
here method needs iterate inputstream:
public static <t> t unmarshal( class<t> docclass, inputstream inputstream )throws jaxbexception { string packagename = docclass.getpackage().getname(); jaxbcontext jc = jaxbcontext.newinstance( packagename ); unmarshaller u = jc.createunmarshaller(); int r; try { while ((r = inputstream.read()) > 0) { //how iterate inputstream? //system.out.println() each line in xml file } } catch (ioexception e) {e.printstacktrace();} object object = u.unmarshal( inputstream );//this throws error. want see inside system.out.println(object.getclass()); system.out.println(jaxbintrospector.getvalue(object).getclass()); jaxbelement<t> doc = (jaxbelement<t>)object;//u.unmarshal( inputstream ); return doc.getvalue(); } just kicks, code calls above method is:
public static void main( string[] args ) { string filename = "c:\\temp\\jaxb\\apps\\create-marshal\\po.xml"; inputstream = getinputstream(filename); try { unmarshal(businessdocument.class, is);} catch (jaxbexception e) {e.printstacktrace();} } public static inputstream getinputstream(string filename){ inputstream = null; try { = new fileinputstream(filename); } catch (filenotfoundexception e) {e.printstacktrace();} catch (ioexception e) {e.printstacktrace();} return is; } edit:
the output in console running @rp- 's code following:
<?xml version="1.0"?> <businessdocument> <typeid root="docroot1" extension="docext1"/> <documenttitle>first document</documenttitle> <entity1> <typeid root="innerroot1" extension="innerext1"/> <entitytitle>inner title</entitytitle> </entity1> </businessdocument> javax.xml.bind.unmarshalexception - linked exception: [org.xml.sax.saxparseexception: premature end of file.] @ javax.xml.bind.helpers.abstractunmarshallerimpl.createunmarshalexception(abstractunmarshallerimpl.java:315) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.createunmarshalexception(unmarshallerimpl.java:505) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal0(unmarshallerimpl.java:206) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal(unmarshallerimpl.java:173) @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:137) @ javax.xml.bind.helpers.abstractunmarshallerimpl.unmarshal(abstractunmarshallerimpl.java:184) @ mainpackage.main.unmarshal(main.java:44) @ mainpackage.main.main(main.java:18) caused by: org.xml.sax.saxparseexception: premature end of file. @ com.sun.org.apache.xerces.internal.util.errorhandlerwrapper.createsaxparseexception(errorhandlerwrapper.java:195) @ com.sun.org.apache.xerces.internal.util.errorhandlerwrapper.fatalerror(errorhandlerwrapper.java:174) @ com.sun.org.apache.xerces.internal.impl.xmlerrorreporter.reporterror(xmlerrorreporter.java:388) @ com.sun.org.apache.xerces.internal.impl.xmlscanner.reportfatalerror(xmlscanner.java:1414) @ com.sun.org.apache.xerces.internal.impl.xmldocumentscannerimpl$prologdriver.next(xmldocumentscannerimpl.java:1059) @ com.sun.org.apache.xerces.internal.impl.xmldocumentscannerimpl.next(xmldocumentscannerimpl.java:648) @ com.sun.org.apache.xerces.internal.impl.xmlnsdocumentscannerimpl.next(xmlnsdocumentscannerimpl.java:140) @ com.sun.org.apache.xerces.internal.impl.xmldocumentfragmentscannerimpl.scandocument(xmldocumentfragmentscannerimpl.java:511) @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(xml11configuration.java:808) @ com.sun.org.apache.xerces.internal.parsers.xml11configuration.parse(xml11configuration.java:737) @ com.sun.org.apache.xerces.internal.parsers.xmlparser.parse(xmlparser.java:119) @ com.sun.org.apache.xerces.internal.parsers.abstractsaxparser.parse(abstractsaxparser.java:1205) @ com.sun.org.apache.xerces.internal.jaxp.saxparserimpl$jaxpsaxparser.parse(saxparserimpl.java:522) @ com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallerimpl.unmarshal0(unmarshallerimpl.java:200) ... 5 more
in general can use bufferedreader read streams.
bufferedreader in = new bufferedreader(new inputstreamreader(inputstream)); string line = null; while ((line = in.readline()) != null) { system.out.println(line); }
Comments
Post a Comment