gpt4 book ai didi

java - 如何解决第三方 XML 架构冲突?

转载 作者:数据小太阳 更新时间:2023-10-29 02:37:12 25 4
gpt4 key购买 nike

我正在处理一组由第三方编写的架构描述 rune 件。我需要为它们生成 JAXB stub 。每个 XSD 都定义了一种不同的消息类型,以及许多支持的简单和复杂类型。许多类型对于每个 XSD 都是通用的,但作者没有将它们分解到一个单独的 XSD 中,而是选择在每个命名空间中定义它们。当我尝试将使用 xjc 的 XSD 编译到单个包中时,这会造成命名空间冲突。我被迫将它们分成不同的包。问题是这使得它们在应该互换的时候不可互换。我必须做很多额外的转换才能在不同的消息类型中使用来自一种消息类型的数据。

我的问题:有没有什么方法(绑定(bind)自定义?)我可以指示 xjc 为每个共享类型使用一个 java 类,而不是分布在不同包中的唯一类?

这是一个简化的例子。我有两个 XSD,一个用于插入消息,另一个用于响应消息。响应旨在引用插入消息。

<!-- insert.xsd -->
<xs:schema
xmlns="msg.insert"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="msg.insert">

<xs:element name="Message" type="Message" />

<xs:complexType name="Message">
<xs:sequence>
<xs:element
maxOccurs="1"
minOccurs="1"
name="MessageId"
type="Identifier" />

<xs:element
maxOccurs="1"
minOccurs="1"
name="SequenceId"
type="Identifier" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="Identifier">
<xs:sequence>
<xs:element
maxOccurs="1"
minOccurs="1"
name="ID"
type="xs:string" />

<xs:element
maxOccurs="1"
minOccurs="1"
name="Created"
type="xs:dateTime" />
</xs:sequence>
</xs:complexType>

</xs:schema>

这是第二个 XSD...

<!-- response.xsd -->
<xs:schema
xmlns="msg.response"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="msg.response">

<xs:element name="Message" type="Message" />

<xs:complexType name="Message">
<xs:sequence>
<xs:element
maxOccurs="1"
minOccurs="1"
name="MessageId"
type="Identifier" />

<xs:element
maxOccurs="1"
minOccurs="1"
name="SequenceId"
type="Identifier" />

<xs:element
maxOccurs="1"
minOccurs="1"
name="ReferenceId"
type="Identifier" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="Identifier">
<xs:sequence>
<xs:element
maxOccurs="1"
minOccurs="1"
name="ID"
type="xs:string" />

<xs:element
maxOccurs="1"
minOccurs="1"
name="Created"
type="xs:dateTime" />
</xs:sequence>
</xs:complexType>

请注意 Identifier 复杂类型在两个模式中是相同的。它可以而且应该在消息类型之间可以互换。但是当我这样运行 xjc 时......

xjc -d java -p example.msg insert.xsd response.xsd

...我在 Message、Identifier 和 ObjectFactory 类上发生冲突,如下所示。

[ERROR] A class/interface with the same name "example.msg.Message" is already in use. Use a class customization to resolve this conflict.
line 8 of insert.xsd

[ERROR] (Relevant to above error) another "Message" is generated from here.
line 8 of response.xsd

[ERROR] A class/interface with the same name "example.msg.Identifier" is already in use. Use a class customization to resolve this conflict.
line 15 of insert.xsd

[ERROR] (Relevant to above error) another "Identifier" is generated from here.
line 16 of response.xsd

[ERROR] Two declarations cause a collision in the ObjectFactory class.
line 8 of insert.xsd

[ERROR] (Related to above error) This is the other declaration.
line 8 of response.xsd

我完全理解为什么 xjc 会提示,我正试图找到一种方法来哄 xjc 使用通用类作为标识符类型,并解决 ObjectFactory 类中的冲突。一种解决方案是将常见类型分解到单独的命名空间中,并从每种消息类型的 XSD 中引用它们,但如前所述,这些都是由第三方编写的,我无法更改它们。

现在我正在将每个 XSD 编译成一个单独的 java 包。这可行,但非常非常麻烦。

错误输出表明有一种方法可以通过绑定(bind)定制来实现这一点,但到目前为止我还没有想出如何实现它。谁能指出我正确的方向?

最佳答案

您只需在wsimport 命令中添加以下自定义参数即可解决此问题:

-B-XautoNameResolution (without spaces)

这样,在解析 xml 时,任何名称冲突都会被自动删除。

希望对你有帮助

关于java - 如何解决第三方 XML 架构冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com