I have two applications, one acting as client and the other as server. In server application I generate ObjectFactory and classes using xjc from Eclipse. As a result, one of this classes is called widgetEvenCall. From the xsd:
...
<xs:element name="widgetEventCall">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" ref="tns:widgetEventDescriptor" />
<xs:element minOccurs="0" maxOccurs="unbounded" ref="tns:widgetParameter" />
</xs:sequence>
</xs:complexType>
</xs:element>
JAXB xjc generates the classes WidgetEventCall, WidgetEventDescriptor and WidgetParameter, with their getters and setters.
The client application, which don't have neither those classes nor the ObjectFactory, calls remotely a service on server application, getting as result one XML like:
. . .
<widgetEventCall>
<widgetEventDescriptor> ... </widgetEventDescriptor>
<widgetParameter>...</widgetParameter>
<widgetParameter>...</widgetParameter>
. . .
</widgetEventCall>
Luckily, client application has access to the .xsd definition. My question is: Is possible, having the xml content and the xsd definition, to create the objects for widgetEventCall, widgetEventDescriptor and widgetParameter like if they were created by xjc, including getters and setters, keeping the client application with no knowledge about them, using exclusively reflection? Is there one automated way to reach this?
my goal is to use this result into a JSP file, i.e. putting the object into request and accessing it like widgetEventCall.widgetParameter[0].someProperty, so I need the getters to be generated.
Thanks in advance.
Joan.
You could use EclipseLink MOXy's Dynamic JAXB for this use case (I'm the MOXy tech lead).
Create the Dynamic JAXB Context:
The JAXBContext can be bootstrapped from an XML:
FileInputStream xsdInputStream = new FileInputStream("src/example/customer.xsd");
DynamicJAXBContext jaxbContext =
DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
Unmarshal the XML:
Then you use an unmarshaller to convert the XML into objects:
FileInputStream xmlInputStream = new FileInputStream("src/example/dynamic/customer.xml");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(xmlInputStream);
Interact with the Data:
The instance of DynamicEntity you get back is a generic object with get/set methods that take a property name. The property name corresponds to what would have been generated on the static class by XJC.
DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set("street", "1 Any Street").set("city", "Any Town");
customer.set("address", address);
Marshal the Object:
Then you use a marshaller to convert the XML into objects:
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
For more information see:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With