• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Not able to get default value of XSD element in Java object

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have couple of XML Schema, some elements in those schema has the default value in element tag. I have generated the java class files using XJC tool (provided by the JDK1.6). So I have complied all java files and build a jar out of that and put that into classpath.





So when I created a object of one of the class I could not get the default value. Every time I get is null. I also put nillable true in those element, but nothing happened.



Please someone could help me to do that.

Thanks
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,
I think, this default value in @XMLElement will be used when unmarshalling XML document to JAXB class instance.
You should add this value to Doc constructor, if you need language to be initialized after new Doc().
I mean this:
 
ajay solanki
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anastasia,

Thanks for reply, but there is problem, I am not generating classes for XSD and it should be not touch manually. So when I use a tool to generate classes it will not add any constructor. I may not be aware that how to add constructor using a tool.

Thanks
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>I am not generating classes for XSD and it should be not touch manually.
The question seems to concern two settings rather than one, namely, the fixed="yes" on an element's text content and the default="eng" again on an element's text content. For the fixed="yes", you may have an impass without touching the source code of the class (Doc.java). For default value, you may have a better chance if you are determined to do that.

It is said, as an excuse or whatever, that the Marshaller will not guarantee or that "there is no requirement that the Java content tree be valid with respect to its original schema in order to marshal it back into XML data." However, default setting concerns with a value constraint that if the marshller first output and then re-unmarshal in tandem, this time with validation in place, to a java content tree, that default value will be recognized and set automatically if the original value (a null) is not altered before marshalling.

Hence, a solution would be to leave it null (do not alter it) and marshal the content tree all done in memory and then unmarshal that output stream (after turning it into an input stream). The resultant content tree will have that default value all set and can be used by the application.
 
Anastasia Klimchuk
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, if Doc source cannot be changed, you can create a DocFactory to create Doc object not through constructor, but through factory method.
Something like


Wherever you want a Doc instance, do this
 
ice is for people that are not already cool. Chill with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic