来自 xsd 的动态 java bean

发布于 2024-09-28 17:09:58 字数 1286 浏览 3 评论 0 原文

我有两个应用程序,一个充当客户端,另一个充当服务器。在服务器应用程序中,我使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2024-10-05 17:09:58

对于此用例,您可以使用 EclipseLink MOXy 的动态 JAXB(我是 MOXy技术主管)。

创建动态 JAXB 上下文:

JAXBContext 可以从 XML 引导:

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

解组 XML:

然后使用解组器将 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);

有关详细信息,请参阅:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文