Wolfgang Laun-2 wrote:
>
> Assuming classes in packages each containing a fiel jaxb.index
> (see JAXBContext.newInstance), you might marshal arbitrary
> classes as shown below. Works with any class or superclass,
> so java.lang.Object is OK, too.
>
> import javax.xml.namespace.QName;
> import packa.*;
> import packb.*;
>
> public class Main {
> Main(){
> }
>
> <T> JAXBElement<T> wrap( String ns, String tag, T o ){
> QName qtag = new QName( ns, tag );
> Class<?> clazz = o.getClass();
> @SuppressWarnings( "unchecked" )
> JAXBElement<T> jbe = new JAXBElement( qtag, clazz, o );
> return jbe;
> }
>
> void marshal() throws Exception {
>
> ClassA a = new ClassA();
> a.setFa1( 42 );
> ClassB b = new ClassB();
> b.setFb1( "baked bean" );
>
> JAXBContext jc = JAXBContext.newInstance( "packa:packb" );
> Marshaller m = jc.createMarshaller();
> m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
>
> JAXBElement<ClassA> jba = wrap( "http://packa", "BeanA", a );
> m.marshal( jba, System.out );
> m.setProperty( Marshaller.JAXB_FRAGMENT, true );
>
> JAXBElement<?> jbb = wrap( "http://packb", "BeanB", (Object)b );
> m.marshal( jbb, System.out );
> }
>
>
> public static void main( String[] args ) throws Exception {
> Main main = new Main();
> main.marshal();
> }
> }
>
> Output:
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <ns2:BeanA xmlns:ns2="http://packa">
> <fa1>42</fa1>
> </ns2:BeanA>
>
> <ns2:BeanB xmlns:ns2="http://packb">
> <fb1>baked bean</fb1>
> </ns2:BeanB>
>
> -W
>
Hello!
thank you very much for your reply: excuse me can you clarify just this:
>Assuming classes in packages each containing a fiel jaxb.index
Do you mean I need to add a field/annotation to the superclass so that it
can marshall fields of the subclass ?
sorry, I didn't understand that.... :-<
thanks for your patience
On Fri, Mar 6, 2009 at 12:27 PM, joshua1970 <joshua1970j_at_libero.it> wrote:
>
> Hi all !
> I'm pretty new to JAXB, however I'm using just the basic features to
> marshal
> my Entity Beans
> into XML. This method works fine to marshal my EntityBean called
> "MyEntity"
> into XML.
>
> StringWriter strWriter = new StringWriter();
> JAXBContext context = null;
> Marshaller marshaller = null;
> try {
> context =
> JAXBContext.newInstance(BaseComponent.class);
> marshaller = context.createMarshaller();
> marshaller.setProperty( Marshaller.JAXB_FRAGMENT,
> Boolean.TRUE );
> } catch (JAXBException e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
> Iterator <MyEntity> iter = list.iterator();
> while (iter.hasNext()) {
> try {
> MyEntity ejb = iter.next();
> marshaller.marshal(ejb , strWriter);
>
> } catch (JAXBException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
>
> My question is : how can I adapt this to all my Entity Beans ? I have
> tried
> first adding a top interface to all my Entity Beans and replacing all
> MyEntity with MyEntityItf in the code
>
> ex
> Iterator <MyEntity> iter = list.iterator();
> with
> Iterator <MyEntityItf> iter = list.iterator();
>
> However I get a runtime error which warns me that I cannot use interfaces
> with JAXB.
> Ok, I have tried extending an empty Class as placeorder but I get an empty
> XML and the error message:
>
> [com.sun.istack.SAXException2: unable to marshal type
> "ejb.entity.MyEntity"
> as an element because it is missing an @XmlRootElement annotation]
>
> Can anybody give me a clue how to solve this problem ?
> thanks a lot
> john
>
>
> --
> View this message in context:
> http://www.nabble.com/Marshalling-a-superclass-tp22369487p22369487.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
:-(
--
View this message in context: http://www.nabble.com/Marshalling-a-superclass-tp22369487p22373508.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.