该 XSD 部分来自:http://www.iana.org/assignments/xml-registry/schema/netconf.xsd http://www.iana.org/assignments/xml-registry/schema/netconf.xsd
<xs:complexType name="rpcType">
<xs:sequence>
<xs:element ref="rpcOperation"/>
</xs:sequence>
<xs:attribute name="message-id" type="messageIdType" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
<xs:element name="rpc" type="rpcType"/>
NETCONF中函数调用的核心是XML文档的节点。我很好奇为什么它不是这样的:
<xs:element name="rpcType">
<xs:complexType>
<xs:sequence>
<xs:element ref="rpcOperation"/>
</xs:sequence>
<xs:attribute name="message-id" type="messageIdType" use="required"/>
<xs:anyAttribute processContents="lax"/>
</xs:complexType>
</xs:element>
原因是,在 #1 中,当尝试编组 bean(在 jaxb2 中)时,我得到了异常:
[com.sun.istack.SAXException2: unable to marshal type "netconf.RpcType" as an element because it is missing an @XmlRootElement annotation]
我一直在阅读this http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html文章看了一遍又一遍,真的无法理解其中的区别,以及为什么它会是#1与#2......
这并不明显,我同意你。这取决于类型与元素的决定。
当你有类似的东西时
<xs:element name="rpcType">
<xs:complexType>
这本质上是一种“匿名类型”,并且是一种永远不会出现在元素内部以外的任何地方的类型rpcType
。由于这种确定性,XJC 知道该类型将始终具有名称rpcType
,因此生成一个@XmlRootElement
它的注释,带有rpcType
name.
另一方面,当你有
<xs:complexType name="rpcType">
那么这定义了一个可重用的类型,它可能被几个不同的元素引用。在您的架构中,它仅由一个元素引用这一事实是无关紧要的。因为这不确定,XJC 对冲其赌注并且不产生@XmlRootElement
.
JAXB 参考实现有一个专有的 XJC 标志,称为「简单绑定模式」 http://weblogs.java.net/blog/kohsuke/archive/2006/03/simple_and_bett.html除其他外,它假设您正在编译的模式永远不会被扩展或与另一个模式组合。这允许它做出某些假设,所以如果它看到一个命名的complexType
只被一个人使用element
,那么往往会生成@XmlRootElement
for it.
现实比这更加微妙和复杂,但在 90% 的情况下,这是一个充分的解释。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)