java - Two Element form of Unmarshaller.unmarshal() -



java - Two Element form of Unmarshaller.unmarshal() -

i have seen next syntax in various stack overflow postings , in blog entries:

jaxbelement<someclass> sc = unmarshaller.unmarshal(is, someclass.class);

so why eclipse give me compilation error when seek utilize syntax? , why syntax not in api, can read at link?

here compilation error:

the method unmarshal(node, class<t>) in type unmarshaller not applicable arguments (fileinputstream, class<someclass>)

here finish method utilize above syntax:

public void unmarshal() throws filenotfoundexception{ unmarshaller unmarshaller; seek { jaxbcontext ctx = jaxbcontext.newinstance(objectfactory.class); unmarshaller = ctx.createunmarshaller(); fileinputstream = new fileinputstream("path/to/some.xml"); jaxbelement<someclass> sc = unmarshaller.unmarshal(is, someclass.class);//error here system.out.println("title is: "+sc.getvalue().gettitle()); } grab (jaxbexception e) {e.printstacktrace();} }

this syntax given in examples developer needs unmarshal xml not contain defined root element. 1 illustration sayantam's reply to question.

the error due wrong type argument, fileinputstream instead of node.

this method more appropriate unmarshal piece of xml. when want parse entire file, utilize unsmarshal(file) method.

public void unmarshal() throws filenotfoundexception{ unmarshaller unmarshaller; seek { jaxbcontext ctx = jaxbcontext.newinstance(objectfactory.class); unmarshaller = ctx.createunmarshaller(); fileinputstream = new fileinputstream("path/to/some.xml"); someclass sc = (someclass) unmarshaller.unmarshal(is); system.out.println("title is: "+sc.getvalue().gettitle()); } grab (jaxbexception e) {e.printstacktrace();} }

if don't want create cast, seek this:

public void unmarshal() throws filenotfoundexception{ unmarshaller unmarshaller; seek { jaxbcontext ctx = jaxbcontext.newinstance(objectfactory.class); unmarshaller = ctx.createunmarshaller(); xmlinputfactory xmlinputfactory = xmlinputfactory.newfactory(); streamsource streamsource = new streamsource("path/to/some.xml"); xmlstreamreader xmlstreamreader = xmlinputfactory.createxmlstreamreader(streamsource); jaxbelement<someclass> sc = unmarshaller.unmarshal(xmlstreamreader, someclass.class);//error here system.out.println("title is: "+sc.getvalue().gettitle()); } grab (jaxbexception e) {e.printstacktrace();} }

java xml jaxb xsd

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -