Inserting code with XJC+xsd+jxb using the options “ -Xinject-code -extension ”

北城余情 提交于 2019-12-27 17:43:29

问题


Im' trying to use the extension "-Xinject-code" of xjc to add some code to my generated classes. For the following simple xsd schema...

<?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>

.. I've associated the following binding:

<?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>

Running xjc produces the following output:

$ 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

but the file 'generated/MyItem.java' doesn't contain the new method "toString". How should I fix this ? What is that message "unknown location" ?

Note:

$ xjc -version
xjc 2.2.4

EDIT:

changing xsd:element to xsd:complexType does the job:

<?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>

and

<?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>

I can now see the code in file "generated/MyItemType.java"

$ tail  generated/MyItemType.java


            @Override
            public String toString() { return this.getName();}

}

but how can I tell xjc to generate the code without changing the xsd file ?


回答1:


Please try /xs:schema/xs:element[@name='MyItem']/xs:complexType.



来源:https://stackoverflow.com/questions/12262289/inserting-code-with-xjcxsdjxb-using-the-options-xinject-code-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!