JAXB 问题:nameXmlTransform typeName 前缀不起作用 [英] Issue with JAXB: nameXmlTransform typeName prefix not working

查看:23
本文介绍了JAXB 问题:nameXmlTransform typeName 前缀不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将多个模式转换为 Java 代码.模式都是相似的;例如,每个都有一个 TXLife 根对象.如果每个模式生成的代码具有唯一的类名,那么管理代码会更容易.我可以使用包"绑定将每个模式中的代码放入自己的包中,但是当我尝试使用前缀"绑定来更改类名时,它会忽略它.

这是我的 schema_bindings.xml 文件:

<块引用>

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0"><jaxb:bindings schemaLocation="schemas/HI_Request.xsd" node="/xsd:schema"><jaxb:schemaBindings><jaxb:package name="com.mycompany.hi"/><jaxb:typeName prefix="Hi_"/></jaxb:nameXmlTransform></jaxb:schemaBindings></jaxb:绑定></jaxb:bindings> 

当我运行 xjc 命令时,我得到(我必须修改 xjc.bat 文件中的类路径才能使其工作):

<块引用>

C:\test>\progs\Java\jaxb-ri-2.2.7\bin\xjc.bat -extension -d src -b schema_bindings.xml schemas

<前>解析模式...编译模式...com\mycompany\hi\Holding.javacom\mycompany\hi\InquiryLevel.javacom\mycompany\hi\KeyedValue.javacom\mycompany\hi\OLifE.javacom\mycompany\hi\ObjectFactory.javacom\mycompany\hi\Policy.javacom\mycompany\hi\TXLife.javacom\mycompany\hi\TXLifeRequest.javacom\mycompany\hi\TransMode.javacom\mycompany\hi\TransSubType.javacom\mycompany\hi\TransType.java

我希望每个 java 文件(以及里面的类)都被命名为Hi_<name>".Jaxb 似乎完全无视我的前缀"规范.我在绑定文件上尝试了几种变体.我还使用 Ant xjc 任务尝试了相同的绑定,所有结果都相同.

我可以处理这些结果,但这意味着处理来自一个模式的输入并将输出生成到另一个模式的代码必须使用完全限定的类名来引用对象,这很尴尬.

解决方案

TL;DR

对应于从命名复杂类型生成的类.您可以通过添加 <jaxb:elementName prefix="Hi_"/> 来影响从全局元素生成的类来执行以下操作:

<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema"><jaxb:schemaBindings><jaxb:package name="com.mycompany.hi"/><jaxb:nameXmlTransform><jaxb:typeName prefix="Hi_"/><jaxb:elementName prefix="Hi_"/></jaxb:nameXmlTransform></jaxb:schemaBindings></jaxb:bindings></jaxb:bindings>

<小时>

完整示例

下面是一个完整的例子.

schema.xsd

下面的架构有一个全局元素和一个命名的复杂类型.

<元素名称="全局元素"><复杂类型><序列><元素名称=foo"类型=字符串"/></序列></complexType></元素><complexType name="NamedComplexType"><序列><元素名称=条"类型=字符串"/></序列></complexType></架构>

binding.xml

<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema"><jaxb:schemaBindings><jaxb:package name="com.mycompany.hi"/><jaxb:nameXmlTransform><jaxb:typeName prefix="Type_"/><jaxb:elementName prefix="Element_"/></jaxb:nameXmlTransform></jaxb:schemaBindings></jaxb:bindings></jaxb:bindings>

XJC 电话

xjc -b binding.xml schema.xsd

输出

我们看到全局元素对应的类以Element_为前缀,命名复杂类型对应的类以Type为前缀.ObjectFactorypackage-info 不是域模型的一部分,它们被 JAXB 用于元数据,因此它们的名称不受影响.

解析模式...编译模式...com/mycompany/hi/Element_GlobalElement.javacom/mycompany/hi/ObjectFactory.javacom/mycompany/hi/Type_NamedComplexType.javacom/mycompany/hi/package-info.java

I wish to convert several schemas into Java Code. The schemas are all similar; for example, each one has a TXLife root object. It would be easier to manage the code if each schema generated code with unique class-names. I can use the "package" binding to put the code from each schema into its own package, but when I try to use the "prefix" binding to change the class names, it ignores it.

Here is my schema_bindings.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0">
   <jaxb:bindings schemaLocation="schemas/HI_Request.xsd" node="/xsd:schema">
      <jaxb:schemaBindings>
         <jaxb:package name="com.mycompany.hi"/>
         <jaxb:nameXmlTransform>
            <jaxb:typeName prefix="Hi_"/>
         </jaxb:nameXmlTransform>
      </jaxb:schemaBindings>
   </jaxb:bindings>
</jaxb:bindings> 

When I run the xjc command I get (I had to modify the classpath inside the xjc.bat file in order to get it to work):

C:\test>\progs\Java\jaxb-ri-2.2.7\bin\xjc.bat -extension -d src -b schema_bindings.xml schemas

parsing a schema...
compiling a schema...
com\mycompany\hi\Holding.java
com\mycompany\hi\InquiryLevel.java
com\mycompany\hi\KeyedValue.java
com\mycompany\hi\OLifE.java
com\mycompany\hi\ObjectFactory.java
com\mycompany\hi\Policy.java
com\mycompany\hi\TXLife.java
com\mycompany\hi\TXLifeRequest.java
com\mycompany\hi\TransMode.java
com\mycompany\hi\TransSubType.java
com\mycompany\hi\TransType.java

What I was hoping for is that each java file (and the class inside) would be named "Hi_<name>". Jaxb seems to be completely ignoring my "prefix" specification. I have tried several variations on the bindings file. I have also tried the same bindings using the Ant xjc task, all with the same results.

I can work with these results, but it would mean that code that processes input from one schema and produces output to another schema would have to use fully-qualified class names to refer to the objects, which is awkward.

解决方案

TL;DR

<jaxb:typeName prefix="Hi_"/> corresponds to the classes generated from named complex types. You could do the following by adding <jaxb:elementName prefix="Hi_"/> to affect the classes generated from global elements:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0">
   <jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
      <jaxb:schemaBindings>
         <jaxb:package name="com.mycompany.hi"/>
         <jaxb:nameXmlTransform>
            <jaxb:typeName prefix="Hi_"/>
            <jaxb:elementName prefix="Hi_"/>
         </jaxb:nameXmlTransform>
      </jaxb:schemaBindings>
   </jaxb:bindings>
</jaxb:bindings> 


Full Example

Below is a complete example.

schema.xsd

The schema below has a global element and a named complex type.

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/schema" 
    xmlns:tns="http://www.example.org/schema" 
    elementFormDefault="qualified">

    <element name="GlobalElement">
        <complexType>
            <sequence>
                <element name="foo" type="string"/>
            </sequence>
        </complexType>
    </element>

    <complexType name="NamedComplexType">
        <sequence>
            <element name="bar" type="string" />
        </sequence>
    </complexType>

</schema>

binding.xml

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0">
   <jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
      <jaxb:schemaBindings>
         <jaxb:package name="com.mycompany.hi"/>
         <jaxb:nameXmlTransform>
            <jaxb:typeName prefix="Type_"/>
            <jaxb:elementName prefix="Element_"/>
         </jaxb:nameXmlTransform>
      </jaxb:schemaBindings>
   </jaxb:bindings>
</jaxb:bindings> 

XJC Call

xjc -b binding.xml schema.xsd

Output

We see that the class corresponding to the global element was prefixed with Element_ and the class corresponding to the named complex type was prefixed with Type. ObjectFactory and package-info are not part of the domain model and are leveraged by JAXB for metadata so their names were not affected.

parsing a schema...
compiling a schema...
com/mycompany/hi/Element_GlobalElement.java
com/mycompany/hi/ObjectFactory.java
com/mycompany/hi/Type_NamedComplexType.java
com/mycompany/hi/package-info.java

这篇关于JAXB 问题:nameXmlTransform typeName 前缀不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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