Change xjc generated Java field name from 'value' to 'name'

I am working on an XML Schema and I would like to change the field name in the generated class from 'value' to 'name'. This is what I would like my StackOverflow.xsd file to look like:

<?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>

      

As you can see, I tried to add a jaxb: property to change the field name "value" to "name", but XJC complains that "Specified property setting is not used". Here is the command I ran

$ 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.

      

If I remove this section from the XSD file:

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

      

then this is the Action.java file generated by

//
// 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;
    }

}

      

As you can see, the @XmlValue field is called "value", but I'd rather be called "name" or "actionName", but I can't figure out how. I would appreciate any help someone can provide.

thank

+3


source to share





All Articles