使用选项 " 插入带有 XJC+xsd+jxb 的代码-Xinject-code -extension " [英] Inserting code with XJC+xsd+jxb using the options " -Xinject-code -extension "

查看:16
本文介绍了使用选项 " 插入带有 XJC+xsd+jxb 的代码-Xinject-code -extension "的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 xjc 的扩展名 "-Xinject-code" 将一些代码添加到我生成的类中.对于以下简单的 xsd 架构...

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>

运行 xjc 产生以下输出:

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

但是文件generated/MyItem.java"不包含新方法toString".我应该如何解决这个问题?什么是未知位置"消息?

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

注意:

$ xjc -version
xjc 2.2.4

将 xsd:element 更改为 xsd:complexType 即可:

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>

<?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"中的代码

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

$ tail  generated/MyItemType.java


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

}

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

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

推荐答案

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

这篇关于使用选项 &quot; 插入带有 XJC+xsd+jxb 的代码-Xinject-code -extension &quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆