来自xsd的动态java bean [英] Dynamic java bean from xsd

查看:239
本文介绍了来自xsd的动态java bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个应用程序,一个作为客户机,另一个作为服务器。在服务器应用程序中,我使用Eclipse中的xjc生成ObjectFactory和类。因此,其中一个类称为widgetEvenCall。从xsd:

  ... 
< xs:element name =widgetEventCall>
< xs:complexType>
< xs:sequence>
< xs:element minOccurs =1maxOccurs =1ref =tns:widgetEventDescriptor/>
< xs:element minOccurs =0maxOccurs =unboundedref =tns:widgetParameter/>
< / xs:sequence>
< / xs:complexType>
< / xs:element>

JAXB xjc使用它们的getter和setter生成类WidgetEventCall,WidgetEventDescriptor和WidgetParameter。



不具有这些类和ObjectFactory的客户端应用程序在服务器应用程序上远程调用服务,获得一个XML,如:

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

幸运的是,客户端应用程序可以访问.xsd定义。我的问题是:有可能的xml内容和xsd定义为widgetEventCall,widgetEventDescriptor和widgetParameter创建对象,如果它们是由xjc创建的,包括getter和setter,保持客户端应用程序不了解它们,使用专属反射?有没有一种自动的方式来达到这个目的?



我的目标是将这个结果用于JSP文件,即将对象放入请求并访问它,如widgetEventCall.widgetParameter [ 0] .someProperty,所​​以我需要生成getter。



提前感谢。

Joan。

解决方案

您可以使用 EclipseLink MOXy 这个用例的动态JAXB(我是MOXy技术主管)。



创建动态JAXB上下文 / p>

可以从XML引导JAXBContext:

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

解密XML:



然后你使用unmarshaller将XML转换成对象:

  FileInputStream xmlInputStream = new FileInputStream(src /例如/动态/ 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);

有关详细信息,请参阅:




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:

这篇关于来自xsd的动态java bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆