我有两个应用程序,一个充当客户端,另一个充当服务器。在服务器应用程序中,我使用 Eclipse 中的 xjc 生成 ObjectFactory 和类。因此,此类之一称为 widgetEvenCall。从 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 生成类 WidgetEventCall、WidgetEventDescriptor 和 WidgetParameter 及其 getter 和 setter。
客户端应用程序既没有这些类,也没有 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。
提前致谢。
琼.
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.
发布评论
评论(1)
对于此用例,您可以使用 EclipseLink MOXy 的动态 JAXB(我是 MOXy技术主管)。
创建动态 JAXB 上下文:
JAXBContext 可以从 XML 引导:
解组 XML:
然后使用解组器将 XML 转换为对象:
与数据:
您返回的 DynamicEntity 实例是一个通用对象,具有采用属性名称的 get/set 方法。属性名称对应于 XJC 在静态类上生成的名称。
编组对象:
然后使用编组器将 XML 转换为对象:
有关详细信息,请参阅:
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:
Unmarshal the XML:
Then you use an unmarshaller to convert the XML into objects:
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.
Marshal the Object:
Then you use a marshaller to convert the XML into objects:
For more information see: