Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

jaxb - How to disable Java Naming Conventions in xjc?

For example sOmE_PROPerty in xsd must be sOmE_PROPerty in java class not someProperty.

I tried to use globalBindings enableJavaNamingConventions="false" but it doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will need to use underscoreBinding="asCharInWord" instead of enableJavaNamingConventions="false":

customer.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema 
    targetNamespace="http://www.example.org/customer" 
    xmlns="http://www.example.org/customer"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified"> 

    <xsd:complexType name="customer">
                <xsd:sequence>
                    <xsd:element name="sOmE_PROPerty" type="xsd:string"/>
                </xsd:sequence>
    </xsd:complexType>

</xsd:schema>

binding.xml

A JAXB binding file is used to customize the schema to Java conversion:

<jaxb:bindings 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">
    <jaxb:globalBindings underscoreBinding="asCharInWord"/>
</jaxb:bindings>

XJC Call

xjc -d out -b binding.xml customer.xsd

Customer

The generated property names now include the underscore character:

package org.example.customer;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

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

    @XmlElement(name = "sOmE_PROPerty", required = true)
    protected String sOmEPROPerty;

    public String getSOmE_PROPerty() {
        return sOmEPROPerty;
    }

    public void setSOmE_PROPerty(String value) {
        this.sOmEPROPerty = value;
    }

}

Without Using binding.xml

If you instead make the following XJC call:

xjc -d out -customer.xsd

You will see that the generated properties do not include the underscore:

package org.example.customer;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

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

    @XmlElement(name = "sOmE_PROPerty", required = true)
    protected String sOmEPROPerty;

    public String getSOmEPROPerty() {
        return sOmEPROPerty;
    }

    public void setSOmEPROPerty(String value) {
        this.sOmEPROPerty = value;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...