我有两个使用JAXB处理的模式.第一个模式已预处理,并使用剧集文件(http://www.java.net/blog/2006/09/05/separate-compilation-jaxb-ri-21)使用此信息.第二个模式导入第一个模式,并再次使用jaxb进行处理.这一切都按预期工作.
但是现在我在第一个模式中有一个元素,在第二个模式中使用了一个引用.
架构a:
<schema elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:test="http://www.example.org/Test/"
targetNamespace="http://www.example.org/Test/">
<element name="type" type="test:MyType"></element>
Run Code Online (Sandbox Code Playgroud)
架构b:
<schema elementFormDefault="qualified"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:second="http://www.example.org/Second/"
xmlns:test="http://www.example.org/Test/"
targetNamespace="http://www.example.org/Second/">
<import namespace="http://www.example.org/Test/" />
<complexType name="SomeType">
<sequence>
<element ref="test:type" minOccurs="1" maxOccurs="unbounded" />
</sequence>
</complexType>
Run Code Online (Sandbox Code Playgroud)
在处理过程中没有任何错误,但两个模式的生成代码提供了相同的方法:
public JAXBElement<EventType> createType(TypeType value)
Run Code Online (Sandbox Code Playgroud)
在运行时,这会导致以下错误:
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of
IllegalAnnotationExceptions
The element name {http://www.example.org/Type/}type has more than one mapping.
Run Code Online (Sandbox Code Playgroud)
如何防止JAXB创建重复的createType方法?
提前致谢!
更新:我在JAXB邮件列表上询问了同样的问题,在该列表中我还发布了一个工作示例.可以在以下位置找到该主题和示例:http://java.net/projects/jaxb/lists/users/archive/2011-03/message/18
在这个列表中,我被建议了一个解决方法,现在我可以按照我喜欢的方式使用模式.但我仍然认为JAXB不应该创建额外的"创建"方法,因为它应该已经在剧集文件中.
我正在尝试JAXBContext类中的各种形式的newInstance方法(我使用的是Oracle JDK 1.7附带的默认Sun JAXB实现).
我不清楚只需将具体类与ObjectFactory类传递给newInstance方法即可.我应该注意到,我纯粹使用JAXB来解析XML文件,即仅在XML-> Java方向.
这是绝对最小的代码,它证明了我的观点:
<?xml version="1.0" encoding="UTF-8"?>
<schema elementFormDefault="qualified"
xmlns ="http://www.w3.org/2001/XMLSchema"
xmlns:a ="http://www.example.org/A"
targetNamespace="http://www.example.org/A">
<element name="root" type="a:RootType"></element>
<complexType name="RootType">
<sequence>
<element name="value" type="string"></element>
</sequence>
</complexType>
</schema>
Run Code Online (Sandbox Code Playgroud)
鉴于上述XSD,以下JAXBInstance.newInstance调用成功创建了一个可解析样本a.xml文件的上下文:
但是,在运行时单独传递example.a.RootType.class失败并出现javax.xml.bind.UnmarshalException:
jc = JAXBContext.newInstance(example.a.RootType.class); // this fails at runtime.
Run Code Online (Sandbox Code Playgroud)
任何人都能解释一下吗?我正在试验这些JAXBContext :: newInstance变体的原因是我偶然发现了这个问题,其中接受的答案包括"基于个别类而不是对象工厂构建JAXB上下文"的选项.我正在使用的示例a.xml和JAXB Java代码在帖子的末尾跟随.
<?xml version="1.0" encoding="UTF-8"?> …Run Code Online (Sandbox Code Playgroud)