Element Defaulting using JAXB - jaxb

I am trying to use JAXB 1.0 provided by JDeveloper 10g version. I have created a Schema definition with elements having default values.
I have following questions:
Whenever I create the Java Content tree using the JAXB generated classes, the java objects should have been created with the default values mentioned in the XML schema. But it doesn't happen. How to make JAXB create the Java objects of an XML document with default values of the elements?
I am trying to add the schemalocation to the marshalled XML document using Marshaller.setProperty( Marshaller.JAXB_SCHEMA_LOCATION, "http://mylocation"); But its not getting reflected in the marshalled XML content.

Have a look at JAXB default value plugin. Some nice additional plugins are mentioned here.

Related

Unmarshall to an existing model

Based on this example Keep child-parent relationship after unmarshalling I'd like to know if it's possible to unmarshall xml file into an existing model (not having the JAXB annotations). My idea is to directly unmarshall into Primefaces Menu Model (https://www.primefaces.org/docs/api/6.0/org/primefaces/model/menu/package-summary.html). Is it possible?
JAXB is configuration by exception, so if your model matches the xml, annotations are not necessary.
Here a blog post and a stackoverflow answer by Blaise Doughan on the topic.
It is possible (if your model is straightforward enough to be JAXB-compatible or with MOXy external mappings), but I would really not recommend it.
If you map to existing model, your XML representation becomes dependent on this existing model. And should the existing model change (like, you update the version of the library you use), you won't be able to unmarshal existing XML and will need migration mechanisms.
From my point of view, it is better to write an XML Schema and compile it to schema-derived classes. Then have a conversion routine to transfrom unmarshalled object structure to the target existing model.

How to document a type in enunciate?

How to get the description field on a type populated in the generated enunciate documentation?
We are generating classes from jaxb using jaxb2-maven-plugin. No matter how I document a element either using the <xsd:documentation></xsd:documentation> or the
<xsd:appinfo>
<jaxb:class>
<jaxb:javadoc>
</jaxb:javadoc>
</jaxb:class>
</xsd:appinfo>
it is overwritten in the generated classes. Can I somehow disable the this auto-generated javadoc from this plugin? Or what does enunciate really expect me to do let me document on a field level?
Note that the comment I write on class-level/type does show up in the generated class and in the enunciated generated documentation.
We are using enunciate (v.1.26.2) and jaxb2-maven-plugin (v. 1.5)
So you're wondering how to get Enunciate to not use the Javadocs on the class?
One way might be to compile the JAXB-generated classes and run Enunciate against the compiled classes instead of the source code.

Generating beans for wix.xsd with jaxb fails since schema seems to be invalid

I'm using JAXB (from Java 7) to generate beans for the schema definitions of WiX. But this generation fails since the wix.xsd (from the source tree of WiX 3.7) contains some regex with word boundary and other delimiters which are not allowed in a regex inside an xml schema definition.
Is there any way to proceed besides changing the schema definitions?

Does JAXB support reading xml tags dynamcally

Is there any method in jaxb to read the xml tags dynamically.For example i have two xml files both have diffrent tagnames and attributes.I want to read these two xml files using a single method.I don want to use getelementByTagname and all.All i need to read the xml tags dynamically.Does JAXB support this or there is any other concepts which can do this.Thanks in advance
You can create a single JAXBContext that represents multiple models. As long as the root objects are annotated with #XmlRootElement (or #XmlElementDecl) then your JAXB (JSR-222) implementation will instantiate the correct objects based on the XML input.
http://blog.bdoughan.com/2010/08/using-xmlanyelement-to-build-generic.html

JAXB : Is the annotation #XmlAccessorType is only for Serialization and nothing to do with Binding of data?

I wanted to know why do we need to specify the Annotation #XmlAccessorType when working with JAXB .
When i googled for this i found out this description from a website stating this
#XmlAccessorType sets default field and property serializability. By default, JAXB serializes public fields and properties. By setting #XmlAccessorType, the bean can choose to only allow annotated fields to be serialized.
Here the author mentions that with this annotation it gives control on serialization .
My question is , so #XmlAccessorType has nothing to do with the JAXB Binding and Unbinding from XML to java and java to XML , and it is all about Serialization only .
JAXB's #XmlAccessorType annotation is only used by JAXB (JSR-222) implementations for determining how to marshal a file to/from XML:
Normally the main decision to be made is between FIELD & PROPERTY/PUBLIC. FIELD is particularly useful when you have logic in your get/set methods that you do not want triggered during marshalling/unmarshalling. To see one way this choice affects the mapping metadata see:
http://blog.bdoughan.com/2012/02/jaxbs-xmltype-and-proporder.html
NONE is a useful choice when you have many unmapped properties and you want to tell your JAXB implementation to only map the fields/properties you have annotated. This can be alot easier than adding a lot of #XmlTransient annotations into your model.
Fore More Information
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

Resources