有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

xsd到JavaBean jaxb字段名

我已经从以下xsd文件创建了JavaBean

人。xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb" elementFormDefault="qualified"
           jxb:version="2.0">

    <xs:annotation>
        <xs:appinfo>
            <jxb:globalBindings>
                <jxb:serializable uid="1"/>
            </jxb:globalBindings>
            <jxb:schemaBindings>
                <jxb:package name="com.thiyanesh"/>
            </jxb:schemaBindings>
        </xs:appinfo>
    </xs:annotation>

    <xs:element name="person">
      <xs:complexType>
        <xs:sequence>
            <xs:element name="id" type="xs:long"/>
            <xs:element name="name" type="xs:string"/>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
</xs:schema>

团队。xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb" elementFormDefault="qualified"
           jxb:version="2.0">

    <xs:import schemaLocation="person.xsd"/>

    <xs:annotation>
        <xs:appinfo>
            <jxb:globalBindings>
                <jxb:serializable uid="2"/>
            </jxb:globalBindings>
            <jxb:schemaBindings>
                <jxb:package name="com.thiyanesh"/>
            </jxb:schemaBindings>
        </xs:appinfo>
    </xs:annotation>

    <xs:element name="team">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="person" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

目标是将人员定义为单独的类,并包括团队类中的人员列表

现在,班级团队包含该字段

List<Person> person;

有没有办法给这个领域起一个不同的名字?说“成员”

List<Person> members;

我可能无法编辑生成的类


共 (1) 个答案

  1. # 1 楼答案

    您只需编辑生成的类并重命名字段(但保留或添加@xmlement(name=“person”)

    还可以自定义架构绑定,例如: http://docs.oracle.com/javase/tutorial/jaxb/intro/custom.html

    但与简单的注释相比,这是一种痛苦。只接受个人而不是会员更容易