Finding JAXB generated class for root element - jaxb

I am new to using JAXB. I need to generate an xml file from its schema, where the schema and element values are input from the user.
My problem is, if I don't know the name of the elements beforehand, how can I use the JAXB generated classes? Is there a generic function, like getRoot() or getChildren() which I can use to traverse through the classes?

Related

jaxb bindings add variables to generated classes

I have a set of schemas that I generate JAXB classes from and I would like JAXB to add a new property to one of the classes for an element that doesn't exist in the schema. How can I do this?

JXB How to use different strategies of code generation

usually, JAXB is used to generate code from an xsd, which generates java classes for xsd complexType with annotations to convert it to xml and vice-versa.
I am trying to achieve something different. I want, to generate a data mapper class for each such xsd element. The mapper will map each field of the generated class with values from another datatype (say from database, or other stream)
so i need to: for every user-defined datatype in xsd, add a method in a DataMapper class map-<XSD-ComplexDataType-Class>() and generate method body.
to achieve this, i think it is not possible to generate this class in a Plugin extending com.sun.tools.internal.xjc.Plugin as in the run method, i wont be able to create a new JDefinedClass
is there any way to add a hook method before Model invokes Plugins ?
thanks,
There are a few things you can do. In my other answer I specificaly meant these:
In a plugin you can write and set your own com.sun.tools.xjc.generator.bean.field.FieldRendererFactory. Field renderers generate a FieldOutlines from CPropertyInfos. This is a step between the model and the outline. So if you want different code generated out of the model, consider implementing your own FieldRendererFactory. You can register a FieldRendererFactory via XJC plugin (see Options.setFieldRendererFactory(...)).
On the class level, you can write your own com.sun.tools.xjc.generator.bean.BeanGenerator and use it for code generation.
You can just use model and generate the code completely on your own. I do this in Jsonix when I produce JavaScript mappings for XML<->JSON.
As for your specific task, I would actually just postprocess the code model in the run method of your plugin. You have everything there - the model, the outline and also the code model (see outline.getCodeModel()). And you can definitely create your JDefinedClasses there, the code model exists already.

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

How to force JAXB marshaller to use xlink references?

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; }

Element Defaulting using 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.

Resources