Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

xml - Converting multiple namespaces XSD to Java

When I needed convert XSD to java, I always was using JAXB. To automate XSD updates on java project maven comes in help with jaxb2-maven-plugin. Standard configuration in pom.xml for this conversation looks like this:

<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>

Where under xsd/xsd_location I put my XSD files and xsd/LocalDateTimeBinding.xjb contains LocalDateTime adapter for JAVA 8+ to avoid joda.time. Sources was generated to java target folder under package name com.example.schema. Everything was working great with simple XSD. This time i have complex one, so don't know what to do. The issue is due multiple namespaces containing same types. XSD example looks like this:

<?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>

Issue is that

../../../../BusinessObjects/Common/AdditionalInformationDataType/V1/AdditionalInformationDataType.xsd

and

../../../../BusinessObjects/Common/AdditionalInformationDataType/V2/AdditionalInformationDataType.xsd

are in different folders, different version XSD types, under different namespace, but both have the same type name.

My current configuration tries to put them on the same package and I get error what file already exists. I can't change XSD (and I don't want to, because it contains over 100 files).

I was looking for some way to put different namespace sources under different packages but with no luck so far.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe that you can fine-tune the package names for each namespace.

I would try two things:

  1. Either remove "packageName" from plugin, or (better)
  2. Define a package name for each namespace using Jaxb customization file
    <jxb:bindings version="1.0"
                   xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema">

        <!--
            Change since version 2.0 of the j-m-p:

            Note that the schemaLocation path must point to the XSD file
            relative to *this* file, rather than the basedir.
        -->
      <jxb:bindings schemaLocation="../xsd/address.xsd" node="//xsd:schema">
          <jxb:schemaBindings>
             <jxb:package name="com.example.myschema"/>
          </jxb:schemaBindings>
      </jxb:bindings>

    </jxb:bindings>

The example is taken from plugins documentation (scroll down to section 6 Using an XML Java Binding file)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...