I use JAXB marshaller to store some java objects as XML files. Some of these objects reference each other, so I unsurprisingly obtain this error:
[com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML
The solution which consists in removing the cycles and use only tree structure is not feasible - I need both navigability directions.
To solve this issue, I would rather use xlink to reference the xml objects instead of copying them in cascade. Is this solution pertinent? Is it possible to do that with JAXB marshaller? How?
You can implement an XLink approach in JAXB using an XmlAdapter. Below are links to various similar answers.
Serialize a JAXB object via its ID?
Using JAXB to cross reference XmlIDs from two XML files
Can JAXB marshal by containment at first then marshal by #XmlIDREF for subsequent references?
I lead the EclipseLink JAXB (MOXy) implementation, and we have the #XmlInverseReference extension for mapping bidirectional relationship that you may be interested in:
http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
Dude, you can note in one of the entity the annotation #XmlTransient, so when unmarch, it will not complain about the cycle problem.
But with thius workaround after unmarch the xml you will have to populate the atribute with the #XmlTransient.
I was reading some paper and find this. You can set #XmlTransient and use the callback method to do something after the unmarch. So you can set the parent to you child.
public void afterUnmarshal(Unmarshaller u, Object parent) {
this.pessoa = (Pessoa) parent; }
Related
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
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
When marshaling some objects into XML, I need to insert an additional field in each of the resulting XML objects - sort of flag. The purpose is not modifying the source objects but inserting that information in the output XML.
Any ideas if this is possible?
There are a few possible approaches:
1. Use an XmlAdapter
You could leverage JAXB's XmlAdapter. Here you would create a version of the classes with the extra field (the adapted classes could extend the original). Then convert between them in the adapter. Since the alternate version of the class would contain the extra field it would marshal out.
http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html
2. Use Binder
If you marshal target is DOM, then you could leverage JAXB's Binder. It is intended for infoset preservation, but after a marshal it does maintain a link between the objects and the DOM nodes. Once the marshal is complete you could use the binder to find an object's associated node and update it.
http://bdoughan.blogspot.com/2010/09/jaxb-xml-infoset-preservation.html
3. Wrap the Output Target
If your output target is something like a ContentHandler or XMLStreamWriter then when the appropriate state is reached you could trigger additional events to be called on the nested marshal target.
The easiest way I can think of would be to use JAXB to marshal to a DOM, and then programmatically insert your extra information into that DOM, then re-marshal the DOM to XML.
Ugly and inefficient, but that's the best I can think of.
I have generated classes using wsimport/wsconsume for my webservice. I am using JAXB marshaller and unmarshaller support. Can any one tell me how these marshaller/unmarshaller uses gnereated classes? say it is geneated - requestFile,Response File,service,ServiceSoap,ObjectFactory, Package-info. Here Service is name of my webservice.
Any help is greatly appreciated.
JAXB uses the annotations on the generated classes to reflectively marshal/unmarshal XML documents. It tries to use sensible defaults if the annotations are missing.
package-info.java is a weird Java5 thing, which allows you to declare package-level annotations. JAXB uses this is do things like declare package-wide type converters, or XML namespaces.
ObjectFactory is a generate class that provides factory methods for the various JAXB types. You usually don't need to use that yourself.
The generated service classes are used by the JAX-WS runtime to assemble the JAXB-bound model objects into actual SOAP requests and responses.
Using JAXB I can create an XSD using code like:
JAXBContext ctx = JAXBContext.newInstance(classes);
ctx.generateSchema(new MySchemaOutputResolver());
That makes a goods XSD describing the structure of all the JAXB objects in the list of classes I pass in, however, I can't figure out how to add other types of XSD restrictions like minOccurs, maxOccurs, pattern, etc.
Is it possible to add annotations which indicate that additional information so that the XSD will include it?
You can use the #XmlElement(required = true) annotation to make an item required. Similar annotations exist for repetition, etc.
See here for annotation classes, the Javadoc has details.