将xjc生成的Java字段名称从“value”更改为“name”

时间:2015-05-20 07:12:57

标签: java xml xsd jaxb schema

我正在研究XML模式,我想将生成的类中的字段名称从“value”更改为“name”。以下是我希望StackOverflow.xsd文件的样子:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:StackOverflow="http://stackoverflow.com"
        xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
        targetNamespace="http://stackoverflow.com"
        attributeFormDefault="unqualified"
        elementFormDefault="qualified"
        jaxb:version="2.1">

    <element name="toolbar">
        <complexType>
            <sequence>
                <element name="action" type="StackOverflow:action" minOccurs="0" maxOccurs="unbounded"/>
            </sequence>
            <attribute type="string" name="id" use="required"/>
        </complexType>
    </element>

    <simpleType name="actionName">
        <restriction base="string">
            <enumeration value="Start"/>
            <enumeration value="Stop"/>
            <enumeration value="Cancel"/>
        </restriction>
    </simpleType>

    <complexType name="action">
        <simpleContent>
            <extension base="StackOverflow:actionName">
                <annotation>
                    <appinfo>
                        <!--This ensures that the field in the Action class will be called 'name' rather than 'value'-->
                        <jaxb:property name="name"/>
                    </appinfo>
                </annotation>
                <attribute name="isActive" type="boolean" default="false"/>
            </extension>
        </simpleContent>
    </complexType>
</schema>

正如您所看到的,我已尝试添加jaxb:属性以将字段'value'的名称更改为'name',但XJC抱怨“未使用指定的属性自定义”。这是我跑的命令

$ xjc StackOverflow.xsd -d tmp/
parsing a schema...
[ERROR] Specified property customization is not used.
  line 33 of file:/D:/dev/workspace-action-engine/jris/jris/branches/action-engine/client/com.agfa.ris.client.lta/src/main/resources/com/agfa/ris/client/lta/listarea/controller/actionsmodel/StackOverflow.xsd

Failed to parse a schema.

如果我从XSD文件中删除此部分:

<annotation>
    <appinfo>
        <!--This ensures that the field in the Action class will be called 'name' rather than 'value'-->
        <jaxb:property name="name"/>
    </appinfo>
</annotation>

,然后这是创建的Action.java文件

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2015.05.20 at 09:05:26 AM CEST 
//


package com.stackoverflow;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
 * <p>Java class for action complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="action">
 *   &lt;simpleContent>
 *     &lt;extension base="&lt;http://stackoverflow.com>actionName">
 *       &lt;attribute name="isActive" type="{http://www.w3.org/2001/XMLSchema}boolean" default="false" />
 *     &lt;/extension>
 *   &lt;/simpleContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "action", propOrder = {
    "value"
})
public class Action {

    @XmlValue
    protected ActionName value;
    @XmlAttribute(name = "isActive")
    protected Boolean isActive;

    /**
     * Gets the value of the value property.
     * 
     * @return
     *     possible object is
     *     {@link ActionName }
     *     
     */
    public ActionName getValue() {
        return value;
    }

    /**
     * Sets the value of the value property.
     * 
     * @param value
     *     allowed object is
     *     {@link ActionName }
     *     
     */
    public void setValue(ActionName value) {
        this.value = value;
    }

    /**
     * Gets the value of the isActive property.
     * 
     * @return
     *     possible object is
     *     {@link Boolean }
     *     
     */
    public boolean isIsActive() {
        if (isActive == null) {
            return false;
        } else {
            return isActive;
        }
    }

    /**
     * Sets the value of the isActive property.
     * 
     * @param value
     *     allowed object is
     *     {@link Boolean }
     *     
     */
    public void setIsActive(Boolean value) {
        this.isActive = value;
    }

}

正如您所看到的,@ xmlValue字段被称为'value',但我希望它被称为'name'或'actionName',但我无法弄清楚如何做到这一点。我很感激有人能提供帮助。

由于

0 个答案:

没有答案