xml将多个名称空间XSD转换为Java
当我需要将XSD转换为java时,我总是使用JAXB。自动化java项目上的XSD更新maven
来自jaxb2-maven-plugin
的帮助。pom.xml
中此对话的标准配置如下所示:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<configuration>
<clearOutputDir>false</clearOutputDir>
<extension>true</extension>
<arguments>
<argument>-Xfluent-api</argument>
</arguments>
</configuration>
<executions>
<execution>
<id>generate-schema</id>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<sources>
<source>xsd/xsd_location</source>
</sources>
<sourceType>xmlschema</sourceType>
<xjbSources>
<xjbSource>xsd/LocalDateTimeBinding.xjb</xjbSource>
</xjbSources>
<packageName>com.example.schema</packageName>
</configuration>
</execution>
</plugin>
在xsd/xsd_location
下,我放置了我的XSD文件,xsd/LocalDateTimeBinding.xjb
包含用于JAVA 8+的LocalDateTime适配器,以避免joda。时间源代码已生成到包名com.example.schema
下的java target
文件夹。使用简单的XSD,一切都很好。这次我有一个复杂的问题,所以不知道该怎么办。问题是由于多个名称空间包含相同的类型。XSD示例如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<xsd:schema xmlns="http://mydaomain.com/BusinessObjects/Common/AdditionalInformationDataListType/V2" targetNamespace="http://example.com/BusinessObjects/Common/AdditionalInformationDataListType/V2"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns0="http://example.com/BusinessObjects/Common/AdditionalInformationDataType/V1" xmlns:ns1="http://example.com/BusinessObjects/Common/AdditionalInformationDataType/V2">
<xsd:import namespace="http://example.com/BusinessObjects/Common/AdditionalInformationDataType/V1" schemaLocation="../../../../BusinessObjects/Common/AdditionalInformationDataType/V1/AdditionalInformationDataType.xsd"/>
<xsd:import namespace="http://example.com/BusinessObjects/Common/AdditionalInformationDataType/V2" schemaLocation="../../../../BusinessObjects/Common/AdditionalInformationDataType/V2/AdditionalInformationDataType.xsd"/>
<xsd:complexType name="AdditionalInformationDataListType">
<xsd:sequence>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element name="additionalInformationDataV1" type="ns0:AdditionalInformationDataType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="additionalInformationDataV2" type="ns1:AdditionalInformationDataType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
问题是
../../../../BusinessObjects/Common/AdditionalInformationDataType/V1/AdditionalInformationDataType.xsd
及
../../../../BusinessObjects/Common/AdditionalInformationDataType/V2/AdditionalInformationDataType.xsd
位于不同文件夹、不同版本的XSD类型、不同命名空间下,但两者具有相同的类型名
我当前的配置试图将它们放在同一个包中,但我得到的错误是文件已经存在。我不能更改XSD(我也不想更改,因为它包含100多个文件)
我一直在寻找将不同名称空间源放在不同包下的方法,但到目前为止运气不佳
# 1 楼答案
我相信您可以微调每个名称空间的包名称
我会尝试两件事:
示例取自插件documentation(使用XML Java绑定文件向下滚动到第6节)
# 2 楼答案
正如Babis Routis在回答中所建议的,我最终使用了jaxb绑定文件。我的模式包含很多文件,所以我不想手动执行。。为了自动化流程,我编写了sh脚本:
它根据目录路径在不同的包中生成每个版本,如果XSD没有版本,它将转到主包。要使其工作,需要从
pom.xml
中删除packageName
,并将其添加到绑定配置中