Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Static JAXBContext instance

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to a create a single instance of a JAXBContext based that would facilitate marshalling many type of classes that all implement
a common interface. I have some success with my current approach.

Below is a snippet of my code as is currently and which works.



Elephant elephant = new Elephant();
Tiger tiger = new Tiger ();

// Marshalling of both the Elephant and Tiger objects work fine as shown below :-

String elephantString = marshallAnimalObject ( elephant );

String tigerString = marshallAnimalObject ( tiger );


My proposed change is to use a static instance of JAXBContext which would be created once only and not everytime the marshallAnimalObject() is invoked.
This instance of JAXBContext would cater for all classes that implement the Animal interface.

However, I am encountering a compiler error when creating the JAXBContext which would cater for all classes that implement the Animal interface.





The problem line is


Clearly, what I am doing is syntax incorrect but can anyone please suggest how I would be able create one JAXBContext instance which I can use to marshall all classes that implement the animal interface.

Would very much appreciate for suggestion to overcome this compiler error please.

Thank you in advance for your help.
Pete
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I got no response from the forum to my query
Was it because the query was difficult ?
Was it because my query was not clear ? - most likely
or ...

Anyway I too was not able to achieve what I had in mind either !

The main objective was to avoid re creating JAXBContext every time I need to marshall or unmarshall a Tiger or Elephant etc,
So my alternative approach has been to cache every new JAXBContext created for a specific class and to use the same instance
everytime.

to that end this is what I done





HTH someone.

Pete

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic