java - How to get output in JAXB (marshaling)? -
java - How to get output in JAXB (marshaling)? -
i tried hello world illustration here , can't see output in programme (in console when using "java" command). wrong do? code of marshal function looks this:
public void marshal() { seek { jaxbelement<greetinglisttype> gl = of.creategreetings( grlist ); jaxbcontext jc = jaxbcontext.newinstance( "hello" ); marshaller m = jc.createmarshaller(); m.marshal( gl, system.out ); } catch( jaxbexception jbe ){ // ... } } i tried tried set output file this:
public void marshal() { seek { jaxbelement<greetinglisttype> gl = of.creategreetings( grlist ); jaxbcontext jc = jaxbcontext.newinstance( "hello" ); fileoutputstream fos = new fileoutputstream("plik.xml"); marshaller m = jc.createmarshaller(); //m.marshal( gl, system.out ); m.marshal(gl, fos); fos.close(); } catch( jaxbexception jbe ){ // ... } catch( ioexception ioe ){ // ... } } but didn't work out. have got solutions?
edit: after printing stack trace gave me this, looks promising:
javax.xml.bind.jaxbexception: "hello" doesnt contain objectfactory.class or jaxb.index @ com.sun.xml.internal.bind.v2.contextfactory.createcontext(contextfactory.java:186) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:616) @ javax.xml.bind.contextfinder.newinstance(contextfinder.java:148) @ javax.xml.bind.contextfinder.find(contextfinder.java:310) @ javax.xml.bind.jaxbcontext.newinstance(jaxbcontext.java:392) @ javax.xml.bind.jaxbcontext.newinstance(jaxbcontext.java:357) @ javax.xml.bind.jaxbcontext.newinstance(jaxbcontext.java:264) @ hello.marshal(hello.java:28) @ hello.main(hello.java:43) i have objectfactory don't know jaxb.index. necessary? how should like?
the demo appears incomplete (missing main method):
public static void main(string[] args) { hello hello = new hello(); hello.make("foo", "bar"); hello.marshal(); } below corrected version:
package hello; import javax.xml.bind.*; public class hello { private objectfactory of; private greetinglisttype grlist; public hello(){ of = new objectfactory(); grlist = of.creategreetinglisttype(); } public static void main(string[] args) { hello h = new hello(); h.make( "bonjour, madame", "fr" ); h.make( "hey, you", "en" ); h.marshal(); } public void make( string t, string l ){ greetingtype g = of.creategreetingtype(); g.settext( t ); g.setlanguage( l ); grlist.getgreeting().add( g ); } public void marshal() { seek { jaxbelement<greetinglisttype> gl = of.creategreetings( grlist ); jaxbcontext jc = jaxbcontext.newinstance( "hello" ); marshaller m = jc.createmarshaller(); m.setproperty(marshaller.jaxb_formatted_output, true); m.marshal( gl, system.out ); } catch( jaxbexception jbe ){ // ... } } } output
<?xml version="1.0" encoding="utf-8"?> <greetings> <greeting language="fr"> <text>bonjour, madame</text> </greeting> <greeting language="en"> <text>hey, you</text> </greeting> </greetings> java jaxb marshalling
Comments
Post a Comment