Given the given xsd schema:

<xsd:complexType name="webCheckType">
    <xsd:sequence maxOccurs="unbounded">
        <xsd:element name="baseUrl" />
        <xsd:element name="beginAt" />
        <xsd:element name="gotoPage" />
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:ID" use="required" />
    <xsd:attribute name="extends" type="xsd:IDREF" use="optional" />
    <xsd:attribute name="displayName" type="xsd:string" use="required" />
</xsd:complexType>

Xjc will produce something like this:

@XmlElements({
    @XmlElement(name = "baseUrl", type = BaseUrl.class),
    @XmlElement(name = "beginAt", type = BeginAt.class),
    @XmlElement(name = "gotoPage", type = GotoPage.class)
})
protected List<Object> baseUrlOrBeginAtOrGotoPage;

How can I change this property name in my binding.xjb file?

有帮助吗?

解决方案

Using JAXB binding customization you can specify the node with a particular name with XPath and customize its property name. The code would be similar to this ( not tested but you get the idea)

<jxb:bindings node=".//xs:element[@name='name']">
        <jxb:property name="toName"/>
</jxb:bindings>

This is my sample xsd - customer.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema  xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="customer">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element
                    name="phone-number"
                    type="xsd:string"
                    maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element> 
</xsd:schema>

Running xjc on this schema creates following class

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "phoneNumber"
})
@XmlRootElement(name = "customer")
public class Customer {

    @XmlElement(name = "phone-number", required = true)
    protected List<String> phoneNumber;

public List<String> getPhoneNumber() {
        if (phoneNumber == null) {
            phoneNumber = new ArrayList<String>();
        }
        return this.phoneNumber;
    }

Now I have create a custom binding

<jxb:bindings schemaLocation="customer.xsd">
    <jxb:bindings node="//xs:element[@name='customer']/xs:complexType/xs:sequence/xs:element[@name='phone-number']">
        <!--<jxb:property name="" collectionType="java.util.LinkedList"/>-->
        <jxb:property name="number"/>
    </jxb:bindings>
</jxb:bindings>

Here I have changed the name of the list property to "number"

When I run xjc with this binding, I get the following Customer class with list property correctly updated

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "number"
})
@XmlRootElement(name = "customer")
public class Customer {

    @XmlElement(name = "phone-number", required = true)
    protected List<String> number;

public List<String> getNumber() {
        if (number == null) {
            number = new ArrayList<String>();
        }
        return this.number;
    }

With your schema you can try following to give your own property name

<jxb:bindings node="//xs:complexType/xs:sequence[xs:element[@name='baseUrl' or @name='beginAt' or @name='gotoPage']]">
            <jxb:property name="yourpropertyname"/>
        </jxb:bindings>

Running this on your schema gives

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "webCheckType", propOrder = {
    "number"
})
public class WebCheckType {

    @XmlElementRefs({
        @XmlElementRef(name = "beginAt", type = JAXBElement.class),
        @XmlElementRef(name = "gotoPage", type = JAXBElement.class),
        @XmlElementRef(name = "baseUrl", type = JAXBElement.class)
    })
    protected List<JAXBElement<Object>> number;
    @XmlAttribute(required = true)
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected String id;
    @XmlAttribute(name = "extends")
    @XmlIDREF
    @XmlSchemaType(name = "IDREF")
    protected Object _extends;
    @XmlAttribute(required = true)
    protected String displayName;

........................
.......................

public List<JAXBElement<Object>> getNumber() {
        if (number == null) {
            number = new ArrayList<JAXBElement<Object>>();
        }
        return this.number;
    }

Of course in my case its JAXBElement because in my xsd I have not given the type for those elements but it should be fine in your case.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top