Dynamic java bean from xsd

I have two applications: one works as a client and the other a server. In a server application, I generate ObjectFactory and classes using xjc from Eclipse. As a result, one of these classes is called widgetEvenCall. From 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 WidgetEventCall, WidgetEventDescriptor and WidgetParameter classes with their receivers and setters.

A client application that does not have these classes or ObjectFactory calls the remote service in the server application, receiving one XML as the result:

. . .
<widgetEventCall>
    <widgetEventDescriptor> ... </widgetEventDescriptor>
    <widgetParameter>...</widgetParameter>
    <widgetParameter>...</widgetParameter>
    . . .
</widgetEventCall>

, .xsd. : , xml- xsd, widgetEventCall, widgetEventDescriptor widgetParameter, xjc, , , ? ?

- JSP , .. widgetEventCall.widgetParameter [0].someProperty, .

. .

+5
1

EclipseLink MOXy JAXB ( - MOXy).

JAXB:

JAXBContext XML:

FileInputStream xsdInputStream = new FileInputStream("src/example/customer.xsd");
DynamicJAXBContext jaxbContext = 
    DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);

Unmarshal XML:

unmarshaller XML :

FileInputStream xmlInputStream = new FileInputStream("src/example/dynamic/customer.xml");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
DynamicEntity customer = (DynamicEntity) unmarshaller.unmarshal(xmlInputStream);

:

DynamicEntity, , get/set, . , XJC.

DynamicEntity address = jaxbContext.newDynamicEntity("org.example.Address");
address.set("street", "1 Any Street").set("city", "Any Town");
customer.set("address", address);

:

XML :

Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);

.

+6

All Articles