编程语言
首页 > 编程语言> > java – 使用选项“-Xinject-code -extension”使用XJC xsd jxb插入代码

java – 使用选项“-Xinject-code -extension”使用XJC xsd jxb插入代码

作者:互联网

我试图使用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();}

}

但是如何在不更改xsd文件的情况下告诉xjc生成代码?

解决方法:

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

标签:extending,xjc,java,jaxb
来源: https://codeday.me/bug/20190918/1811146.html