aspose file tools*
The moose likes XML and Related Technologies and the fly likes Not able to get default value of XSD element in Java object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Not able to get default value of XSD element in Java object" Watch "Not able to get default value of XSD element in Java object" New topic
Author

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

ajay solanki
Ranch Hand

Joined: Feb 25, 2008
Posts: 37
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
Anastasia Klimchuk
Greenhorn

Joined: Sep 21, 2011
Posts: 10
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

Joined: Feb 25, 2008
Posts: 37
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
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 464
    
    2
>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

Joined: Sep 21, 2011
Posts: 10
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to get default value of XSD element in Java object
 
Similar Threads
.xsd schema questions
MS Exchange web service: If the 'nillable' attribute is false in the schema, the 'xsi:nil' attribute
servlets and xml
XML valiadation using schema
Invalid type (xsi:type) is set when XML request is generated from web service client