简体   繁体   English

如何自定义和覆盖jaxb绑定

[英]how to customize and override jaxb bindings

I am working with a set of DTDs from a third party system. 我正在使用第三方系统中的一组DTD。 Our goal is to map the XML request (which conforms to those DTDs into java and then, send an XML response back to the system). 我们的目标是将XML请求映射(将那些DTD符合到Java中,然后将XML响应发送回系统)。

DTDs are written in stone (I don't have any control in changing those). DTD是用石头写的(更改时我没有任何控制权)。

So, in order to map, I converted DTDs to XML Schemas (xsd) using XMLSpy and then, created Jaxb binding classes using XJC compiler. 因此,为了进行映射,我使用XMLSpy将DTD转换为XML Schema(xsd),然后使用XJC编译器创建了Jaxb绑定类。 I am using Java 7. 我正在使用Java 7。

The problem is, the DTDs don't really have a namespace.. and I have 20 different DTDs.. 10 for request and 10 for response. 问题是,DTD确实没有名称空间..我有20个不同的DTD。.10个用于请求,10个用于响应。 When I generated the schemas, I had to do one-one mapping.. and created the same 10 request XSDs and 10 response XSDs. 生成模式时,我必须进行一对一映射..并创建相同的10个请求XSD和10个响应XSD。

Now, the jaxb xjc compiler generated binding classes.. but they are far from practical use. 现在,jaxb xjc编译器生成了绑定类..但是它们远非实际使用。 There is no inheritance 'cus these schemas are not related to each other (although they seem to have similar content - request types and response types). 没有继承-因为这些模式彼此之间不相关(尽管它们似乎具有相似的内容-请求类型和响应类型)。

Can someone please help me if there is a way to customize jaxb bindings to override the default bindings and create more reasonable bindings? 如果可以自定义jaxb绑定以覆盖默认绑定并创建更合理的绑定,有人可以帮我吗?

For example consider this simple case: 例如,考虑以下简单情况:

DTD: DTD:

<!ELEMENT FromDate (#PCDATA)>
<!ATTLIST FromDate
    year CDATA #REQUIRED
    month CDATA #REQUIRED
    day CDATA #REQUIRED
>

Schema that I generated using XMLSpy: 我使用XMLSpy生成的架构:

<xs:element name="FromDate">
    <xs:complexType mixed="true">
        <xs:attribute name="year" use="required"/>
        <xs:attribute name="month" use="required"/>
        <xs:attribute name="day" use="required"/>
    </xs:complexType>
</xs:element>

The binding classes that generated out of XJC compiler (java 1.7): 从XJC编译器(java 1.7)生成的绑定类:

public class FromDate {

    @XmlValue
    protected String content;
    @XmlAttribute(name = "year", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected String year;
    @XmlAttribute(name = "month", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected String month;
    @XmlAttribute(name = "day", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected String day;
    ...
    ...

If you look at how fromDate finally evolved, it doesn't make any sense 'cus just to get the date from this request, I need to do 如果您查看fromDate的最终演变方式,那么仅仅从该请求中获取日期就没有任何意义,我需要

setMyDate(request.getFromDate().getMonth() + request.getFromDate().getDay() + request.getFromDate().getYear());

which obviously doesn't make sense. 这显然没有意义。 Plus, the types are way off. 另外,类型还很遥远。

How can I customize/override jaxb bindings to achieve these two things: 1. inheritance (some kind of abstraction to reduce redundancy) 2. appropriate types 我如何自定义/重写jaxb绑定以实现这两件事:1.继承(某种抽象,以减少冗余)2.适当的类型

Please help. 请帮忙。

OMG someone tries to compile DTDs in 2014. :) OMG有人试图在2014年编译DTD。:)

Few links for you: 很少有适合您的链接:

As another approach I'd suggest converting DTDs to schemas an processing schemas. 作为另一种方法,我建议将DTD转换为处理模式的模式。 Will be better long-term. 长期来看会更好。 DTD support is quite limited DTD支持非常有限

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

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