我正在尝试使用 xjc 的扩展“ - Xinject-code”来为我生成的类添加一些代码。对于以下简单的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
但是'generated / MyItem.java'文件不包含新方法“toString”。我该怎么解决这个问题?什么是“未知位置”的消息?
注意:
$ xjc -version
xjc 2.2.4
修改
将xsd:element更改为xsd:complexType完成工作:
<?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>
我现在可以看到文件“generated / MyItemType.java”
中的代码$ tail generated/MyItemType.java
@Override
public String toString() { return this.getName();}
}
但是如何告诉xjc生成代码而不更改xsd文件?
答案 0 :(得分:4)
请尝试/xs:schema/xs:element[@name='MyItem']/xs:complexType
。