简体   繁体   English

使用不同的模式将数据转换为xml

[英]Transform data to xml with different schemas

I need to convert a List of records(java objs) into XML output. 我需要将记录列表(java objs)转换为XML输出。 The XML schema can change based on the request type and the xml output needs to be in a different format with different elements based on the request parameter (Eg. Request A -> produces xml in format A <A><aa>name</aa></A> Request B -> format B <B><bab>name</bab></B> ). XML模式可以根据请求类型进行更改,并且xml输出需要采用不同的格式,并根据请求参数使用不同的元素(例如,请求A->以A <A><aa>name</aa></A>格式生成xml。 <A><aa>name</aa></A>请求B->格式B <B><bab>name</bab></B> )。 The existing framework uses JAXB but the dataobjects are tightly coupled with one XML schema. 现有框架使用JAXB,但是数据对象与一个XML模式紧密耦合。

We are trying to refactor to make it flexible so that based on the request type, we can generate different xml outputs. 我们正在尝试进行重构以使其具有灵活性,以便基于请求类型,我们可以生成不同的xml输出。 ie same data -> multiple output formats. 即相同的数据->多种输出格式。 We are also expecting more xml schemas, so we need to keep it flexible. 我们还期望有更多的xml模式,因此我们需要保持它的灵活性。

Appreciate if you could please let me know which framework would be most suitable for this type of scenario. 如果可以的话,请让我知道哪种框架最适合这种情况。 What would be a good solution for huge schema and data files. 对于庞大的架构和数据文件,这将是一个好的解决方案。 Thank you! 谢谢!

So you have your Domain Model and one or more Integration Models (output formats). 因此,您拥有域模型和一个或多个集成模型(输出格式)。 I assume, when you create a response (Integration Model), you use some kind of adapter from Domain Model (if not, you should consider this). 我假设,当您创建响应(集成模型)时,您使用了域模型中的某种适配器(如果没有,则应考虑这一点)。 I would keep it this way for multiple Integration Models (response types / output formats). 对于多种集成模型(响应类型/输出格式),我将保持这种方式。

For each response type, have a dedicated Integration Model (eg generate it from a XSD, eg with JAXB) and have an adapter from your Domain Model to this Integration Model. 对于每种响应类型,都有一个专用的集成模型(例如,从XSD生成它,例如使用JAXB),并有一个从您的域模型到该集成模型的适配器。 Then, based on request type, select one of the adapters and produce the response. 然后,根据请求类型,选择一个适配器并产生响应。

If you want it to be really futureproof, you should consider to have your own Integration Model for the app. 如果您希望它真正面向未来,则应考虑为应用程序拥有自己的集成模型。 This will prevent you from making changes to the adapters whenever your Domain Model changes, like this: 这将防止您在域模型更改时对适配器进行更改,例如:

Domain Model -> [trafo] -> Common Integration Model -> [trafos] -> 3rd Party Integration Models 域模型-> [trafo]->通用集成模型-> [trafos]->第三方集成模型

By the way, converters don't need to be implemented in one particular technology, use what best fits a particular case. 顺便说一下,转换器不需要用一种特定的技术来实现,而可以使用最适合特定情况的转换器。 One adapter may use JAXB generated Java classes, other may be a XML Transformation. 一个适配器可以使用JAXB生成的Java类,另一个可以使用XML转换。

You may consider implementing your integration layer with frameworks like Apache Camel . 您可以考虑使用Apache Camel之类的框架来实现集成层。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM