10

我试图使用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

但文件“生成/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 文件的情况下生成代码?

4

1 回答 1

6

请尝试/xs:schema/xs:element[@name='MyItem']/xs:complexType

于 2013-04-16T21:53:39.967 回答