JAXB:如何在 XSD 中指定 attr 类型时更改 XJC 生成的类名称? [英] JAXB: How to change XJC-generated classes names when attr type is specified in XSD?

查看:25
本文介绍了JAXB:如何在 XSD 中指定 attr 类型时更改 XJC 生成的类名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JAXB 的初学者,在使用 xjc 生成 Java 类时遇到了烦人的问题.我得到了这样的 XSD:

I'm a beginner to JAXB and I'm having annoying issues when generating Java classes with xjc. I am provided with a XSD like this:

<xs:element name="item" type="itemType"/>  
...   
<xs:complexType name="itemType">
    <xs:attribute name="id" type="xs:string" use="required">
    ...     
</xs:complexType>

和 xjc 正在生成一个名为 ItemType.java 的类,但我希望名称为 Item.java.也就是说,我希望生成的类就像 XSD 是这样的:

and xjc is generating a class called ItemType.java, but I want the name to be Item.java. That is, I want the generated classes as if the XSD was like this:

<xs:element name="item">
    <xs:complexType>
    <xs:attribute name="id" type="xs:string" use="required">
        ...
    </xs:complexType>
</xs:element>

不会在任何其他元素上重用 itemType,只是构建 XSD 的人喜欢这种方式.我想可能有一种方法可以使用自定义绑定来做到这一点,但我仍然没有找到方法.

There won't be any reuse of itemType on any other element, it's just the people that constructs the XSD likes it this way. I guess there may be a way to do it with custom bindings but I still haven't found how.

有什么帮助吗?

谢谢,米格尔

推荐答案

JAXB 提供了两种方式来实现:

JAXB provides two ways to accomplish this:

1.内联架构注释

您可以使用 JAXB 模式注释来控制类名.

You can use JAXB schema annotations to control the class names.

<xs:schema 
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        jaxb:version="2.1">

    <xs:complexType name="itemType">
        <xs:annotation>
            <xs:appinfo>
                <jaxb:class name="Item"/>
            </xs:appinfo>
        </xs:annotation>
        <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>

</xs:schema>

2.外部绑定文件

这种定制也可以通过外部绑定文件来完成:

This customization can also be done via and external binding file:

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="your-schema.xsd">
            <jxb:bindings node="//xs:complexType[@name='itemType']">
                <jxb:class name="Item"/>
            </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

xjc 命令行是:

xjc -d out -b binding.xml your-schema.xsd

这篇关于JAXB:如何在 XSD 中指定 attr 类型时更改 XJC 生成的类名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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