我在保持课堂重生方面遇到了麻烦.我
我有一个模式A,它在模式B中导入.然后在模式C中导入模式B.
在模式A中,我有两个simpleTypes,它们是字符串的枚举:
<xs:simpleType name="blah_type">
<xs:restriction base="xs:string">
<xs:enumeration value="blah_1"/>
<xs:enumeration value="blah_2"/>
<xs:enumeration value="blah_3"/>
<xs:enumeration value="blah_4"/>
<xs:enumeration value="blah_5"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="another_blah_type">
<xs:restriction base="xs:string">
<xs:enumeration value="another_blah_1"/>
<xs:enumeration value="another_blah_2"/>
<xs:enumeration value="another_blah_3"/>
<xs:enumeration value="another_blah_4"/>
<xs:enumeration value="another_blah_5"/>
</xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)
这些产生了生成的类.在模式C中,与这些枚举对应的类不断重新生成,当然也在错误的位置.我正在以这种方式添加对现有类的引用:(jaxb-creating-modules-for-reuse)
<jaxb:bindings node="//xs:simpleType[@name='blah_type']">
<jaxb:class ref="com.stuff.otherstuff.really.deep.BlahType"/>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)
我有其他类型(complexTypes和非枚举simpleTypes)一切都很好.
谢谢你的帮助.
我有几种情况,JAXB从xsd文件生成的类有一系列类列表,这些类也包含类列表.生成的类在处理这些情况时使用静态内部类,但这会导致多个实例多次存在一个类.
作为一个例子,我有一个存在于多个xsd文件中的Parameter类.在每个实例中,此参数类都相同并包含键值对.为每个xsd文件生成代码时,每个生成的类都包含一个名为Parameter的内部类.
为了减少冗余,我从所有生成的类中删除了Parameter类,重新实现了它,并更新了所有引用.这按预期工作
当我有另一个类,如LogicalDevice,也在几个类中实现时,会出现问题.LogicalDevice包含一个ParameterList.当LogicalDevice被提取到它自己的类,并且引用被更新时,只保留Parameter类中的第一个属性,其余属性为null.
这让我相信静态内部类有一些特殊之处.
这里参考我的xml和代码
<Jetstream xmlns="http://Jetstream.TersoSolutions.com/v1.1/GetPoliciesResponse">
<Header/>
<GetPoliciesResponse>
<PolicyList>
<Policy Id="53bb663f-7ed0-41f8-9757-90ad4e015995" Name="MyPolicy"
DeviceDefinitionId="f3536a6d-4610-4d56-82d9-54fd6602a883" >
<ParameterList>
<Parameter Name="aggregateeventscancount" Value="2"/>
<Parameter Name="aggregateeventscantime" Value="10"/>
<Parameter Name="antenna1rxsensitivity" Value="50"/>
<Parameter Name="antenna1txpower" Value="30"/>
<Parameter Name="antenna2rxsensitivity" Value="50"/>
<Parameter Name="antenna2txpower" Value="30"/>
<Parameter Name="antenna3rxsensitivity" Value="50"/>
<Parameter Name="antenna3txpower" Value="30"/>
<Parameter Name="antenna4rxsensitivity" Value="50"/>
<Parameter Name="antenna4txpower" Value="30"/>
<Parameter Name="commandpollinterval" Value="60"/>
<Parameter Name="dns" Value="0.0.0.0"/>
<Parameter Name="dooropentimelimit" Value="300"/>
<Parameter Name="gateway" Value="0.0.0.0"/>
<Parameter Name="ip" Value="0.0.0.0"/>
<Parameter Name="jetstreamdeviceurl" Value="https://us-device.tersosolutions.com/v1.0/device/"/>
<Parameter Name="lockdownhightemp" Value="127"/>
<Parameter Name="lockdownonacpowerfailure" Value="0"/>
<Parameter Name="lockdownonreaderfailure" Value="0"/>
<Parameter Name="lockdownonhightemp" Value="0"/>
<Parameter Name="logentryeventhightemp" …Run Code Online (Sandbox Code Playgroud) 我有一个xsd,其中嵌入了几个子XSD(使用“ xsd:include”。)(但是每个xsd都属于同一个名称空间)。我被要求在单独的子包(每个子XSD一个子包)而不是一个包中生成jaxb类。
根据到目前为止的jaxb:schemaBindings读物,如果只有那些xsds属于不同的名称空间,我们可以将它们添加到这些单独的xsds中并在单独的包中生成类。
但就我而言,我想在几个子包中为一组属于同一名称空间的xsds创建类。您能帮我使用JAXB做到这一点吗?
我所有的XSD都有以下标头。因此命名空间相同。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:srm="http://www.mycompany.com/srm/"
targetNamespace="http://www.mycompany.com/srm/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0" elementFormDefault="qualified"
attributeFormDefault="unqualified" version="1.0">
Run Code Online (Sandbox Code Playgroud)
假设我的架构文件名为A.xsd,B.xsd和C.xsd。
在C.xsd中,我使用上述标题定义了一些元素(字符串类型)。
在B.xsd中,我使用“ xsd:include”标记包含了C.xsd,然后使用C.xsd中定义的复杂类型定义了complexType。(具有与我上面提到的相同的标头)
在A.xsd中,我使用“ xsd:include”标记包含了B.xsd,然后使用B.xsd中定义的复杂类型定义了complexType定义。(具有我上面提到的相同标头)
我想像下面提到的那样生成JAXB类。
与A.xsd相关的JAXB类在com.generate.packageA包中生成。
与B.xsd相关的JAXB类在com.generate.packageB包中生成。
与C.xsd相关的JAXB类在com.generate.packageC包中生成。
我使用jaxb2-maven-plugin的xjc目标从一组 xsd 文件生成 Java 类。
一个最小的、完整的和可验证的例子是一个带有以下 pom.xml 文件的 Maven 项目:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>jaxb2-maven-episode-test</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>${project.basedir}/src/main/resources/</source>
</sources>
<generateEpisode>false</generateEpisode>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
在src/main/resources/文件夹中有一个名为example.xsd的文件(任何有效的 xsd 文件都可以):
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://tempuri.org/PurchaseOrderSchema.xsd"
targetNamespace="http://tempuri.org/PurchaseOrderSchema.xsd"
elementFormDefault="qualified">
<xsd:element name="PurchaseOrder" type="tns:PurchaseOrderType"/>
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="ShipTo" type="tns:USAddress" maxOccurs="2"/>
<xsd:element name="BillTo" type="tns:USAddress"/> …Run Code Online (Sandbox Code Playgroud)