林”尝试使用XJC的扩展名‘-Xinject代码’一些代码添加到我的生成的类。 对于下面的简单XSD架构...
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyList" >
<xs:complexType>
<xs:sequence>
<xs:element ref="MyItem" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="MyItem">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
..我相关的下列绑定:
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
jxb:extensionBindingPrefixes="ci "
jxb:version="2.1"
>
<jxb:bindings schemaLocation="test.xsd">
<jxb:bindings node="/xs:schema/xs:element[@name='MyItem']">
<ci:code>
@Override
public String toString() { return this.getName();}
</ci:code>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
运行XJC产生以下的输出:
$ xjc -target 2.1 -verbose -Xinject-code -extension -d . -p generated -b test.jxb test.xsd
parsing a schema...
compiling a schema...
[INFO] generating code
unknown location
generated/MyItem.java
generated/MyList.java
generated/ObjectFactory.java
但文件“生成/ MyItem.java”不包含新的方法“的toString”。 我应该怎样解决这个问题? 那是什么消息“未知位置”?
注意:
$ xjc -version
xjc 2.2.4
编辑:
改变XSD:元素为xsd:复杂类型做工作:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="MyItemType">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="MyList" >
<xs:complexType>
<xs:sequence>
<xs:element name="MyItem" type="MyItemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
和
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
jxb:extensionBindingPrefixes="ci "
jxb:version="2.1"
>
<jxb:bindings schemaLocation="test.xsd">
<jxb:bindings node="/xs:schema/xs:complexType[@name='MyItemType']">
<ci:code>
@Override
public String toString() { return this.getName();}
</ci:code>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
我现在可以看到在文件中的代码“生成/ MyItemType.java”
$ tail generated/MyItemType.java
@Override
public String toString() { return this.getName();}
}
但我怎么能告诉XJC生成代码,而无需改变XSD文件?