This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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:
Joined: Feb 25, 2008
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.
>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.
Joined: Sep 21, 2011
Ok, if Doc source cannot be changed, you can create a DocFactory to create Doc object not through constructor, but through factory method.