Java 如何为xjc编写外部绑定文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1707714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write an external binding file for xjc?
提问by tangens
The documentation of JAXB xjcsays:
JAXB xjc的文档说:
-b Specify one or more external binding files to process. (Each binding file must have it's own "-b" switch.) The syntax of the external binding files is extremely flexible. You may have a single binding file that contains customizations for multiple schemas or you can break the customizations into multiple bindings files:
xjc schema1.xsd schema2.xsd schema3.xsd -b bindings123.xjb xjc schema1.xsd schema2.xsd schema3.xsd -b bindings1.xjb -b bindings2.xjb -b bindings3.xjb
In addition, the ordering of the schema files and binding files on the command line does not matter.
-b 指定一个或多个要处理的外部绑定文件。(每个绑定文件必须有它自己的“-b”开关。)外部绑定文件的语法非常灵活。您可能有一个包含多个模式自定义的绑定文件,或者您可以将自定义分解为多个绑定文件:
xjc schema1.xsd schema2.xsd schema3.xsd -b bindings123.xjb xjc schema1.xsd schema2.xsd schema3.xsd -b bindings1.xjb -b bindings2.xjb -b bindings3.xjb
此外,命令行上模式文件和绑定文件的顺序无关紧要。
But where do I find a documentation of this "external binding file"?
但是我在哪里可以找到这个“外部绑定文件”的文档?
回答by Tim Penner
The external binding file is documented on the Customizing JAXB Bindingspage on oracle.com
外部绑定文件记录在 oracle.com上的自定义 JAXB 绑定页面上
Quote:
引用:
External Binding Customization Files
Customizations to JAXB bindings made by means of an external file containing binding declarations take the general form shown below.
<jxb:bindings schemaLocation = "xs:anyURI"> <jxb:bindings node = "xs:string">* <binding declaration> <jxb:bindings> </jxb:bindings>
schemaLocation is a URI reference to the remote schema node is an XPath 1.0 expression that identifies the schema node within schemaLocation to which the given binding declaration is associated. For example, the first schemaLocation/node declaration in a JAXB binding declarations file specifies the schema name and the root schema node:
<jxb:bindings schemaLocation="po.xsd" node="/xs:schema">
A subsequent schemaLocation/node declaration, say for a simpleType element named ZipCodeType in the above schema, would take the form:
<jxb:bindings node="//xs:simpleType[@name='ZipCodeType']">
外部绑定自定义文件
通过包含绑定声明的外部文件对 JAXB 绑定进行的自定义采用如下所示的一般形式。
<jxb:bindings schemaLocation = "xs:anyURI"> <jxb:bindings node = "xs:string">* <binding declaration> <jxb:bindings> </jxb:bindings>
schemaLocation 是对远程模式节点的 URI 引用,它是一个 XPath 1.0 表达式,用于标识 schemaLocation 中与给定绑定声明相关联的模式节点。例如,JAXB 绑定声明文件中的第一个 schemaLocation/node 声明指定模式名称和根模式节点:
<jxb:bindings schemaLocation="po.xsd" node="/xs:schema">
后续的 schemaLocation/node 声明,例如上述模式中名为 ZipCodeType 的 simpleType 元素,将采用以下形式:
<jxb:bindings node="//xs:simpleType[@name='ZipCodeType']">
See also; the JAXB Compiler Options
也可以看看; 在JAXB编译器选项
Another good resource for information on the external binding file is oreilly. An example binding file from oreillyis:
另一个关于外部绑定文件信息的好资源是oreilly。来自oreilly的示例绑定文件是:
Quote:
引用:
Listing 11. Using An External Binding File
<jxb:bindings version="1.0" 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:extensionBindingPrefixes="xjc"> <jxb:bindings schemaLocation="po4.xsd" node="/xs:schema"> <jxb:globalBindings> <xjc:superClass name="com.syh.Shape"/> <xjc:serializable uid="12343"/> </jxb:globalBindings> <jxb:bindings node="//xs:element[@name='Widgets']//xs:complexType//xs:choice"> <jxb:property name="Shapes"/> </jxb:bindings> </jxb:bindings> </jxb:bindings>
清单 11. 使用外部绑定文件
<jxb:bindings version="1.0" 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:extensionBindingPrefixes="xjc"> <jxb:bindings schemaLocation="po4.xsd" node="/xs:schema"> <jxb:globalBindings> <xjc:superClass name="com.syh.Shape"/> <xjc:serializable uid="12343"/> </jxb:globalBindings> <jxb:bindings node="//xs:element[@name='Widgets']//xs:complexType//xs:choice"> <jxb:property name="Shapes"/> </jxb:bindings> </jxb:bindings> </jxb:bindings>