nev*_*219 9 java xml xsd jaxb xjc
我正在使用xjc从XML模式生成Java类,以下是XSD的摘录.
<xs:element name="NameInfo">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element ref="UnstructuredName"/> <!-- This line -->
<xs:sequence>
<xs:element ref="StructuredName"/>
<xs:element ref="UnstructuredName" minOccurs="0"/> <!-- and this line! -->
</xs:sequence>
</xs:choice>
<xs:element ref="SomethingElse" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,生成的类很好,但对于上面的块,我会得到类似的东西:
public List<Object> getContent() {
if (content == null) {
content = new ArrayList<Object>();
}
return this.content;
}
Run Code Online (Sandbox Code Playgroud)
以上评论如下:
* You are getting this "catch-all" property because of the following reason:
* The field name "UnstructuredName" is used by two different parts of a schema. See:
* line XXXX of file:FILE.xsd
* line XXXX of file:FILE.xsd
* To get rid of this property, apply a property customization to one
* of both of the following declarations to change their names:
* Gets the value of the content property.
Run Code Online (Sandbox Code Playgroud)
我在两条线的末尾发表评论.
目前,我认为改变架构并不容易,因为这是由供应商决定的,我不想走这条路(如果可能的话),因为它会减慢进度.
我搜索并找到了这个页面,是外部定制我想做什么?我一直在使用生成的类,所以我不完全熟悉生成这些类的过程."财产定制"的一个简单例子会很棒!只要仍然可以使用模式,生成Java类的替代方法就可以了.
编辑:我应该澄清这两个UnstructuredName
确实是相同的元素.
您还可以使用名为的绑定自定义<xjc:simple />
:
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jxb:version="2.0">
<jxb:globalBindings>
<xjc:simple />
</jxb:globalBindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)
但请注意,这是特定于供应商的(虽然使用了除XJC以外的其他东西;))
更多信息在这里
这里的基本问题是你有一个<xs:sequence>
由 an 组成的<xs:choice>
,在 Java 中翻译为“List
事物的一部分”。Java 的类型结构不够灵活,无法更好地表示这一点。
绑定自定义可能会对您有所帮助,但在这种情况下,我怀疑不会,因为我看不到更好的方法来表示此信息。
我过去使用的另一种技术是首先通过简单的 XSLT 转换传递模式,将组件重新排列为更 JAXB 友好的内容,同时仍然允许文档具有实际的相同结构。这样,您可以“更改”架构而不更改原始架构。