2017-04-05 127 views
0

進出口使用此xjc腳本XJC的Java類的生成,其中字段具有相同的名稱作爲@XmlElement

for %%f in (*.xsd) do (
    xjc -no-header %%f 
) 
pause 

它會生成這個樣子

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "AppData1", propOrder = { 
    "appInstllCd", 
    "appVrsn", 
    "os", 
    "osVrsn", 
    "device" 
}) 
public class AppData1 { 

    @XmlElement(name = "AppInstllCd") 
    protected String appInstllCd; 
    @XmlElement(name = "AppVrsn") 
    protected String appVrsn; 
    @XmlElement(name = "OS") 
    protected String os; 
    @XmlElement(name = "OSVrsn") 
    protected String osVrsn; 
    @XmlElement(name = "Device") 
    protected String device; 

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

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

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

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

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

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

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

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

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

    /** 
    * Sets the value of the device property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setDevice(String value) { 
     this.device = value; 
    } 
} 
類從.xsd文件創建Java類

我想知道如果我可以以某種方式更改我的腳本,這使得我的班級字段與@XmlElement具有相同的名稱,例如結果將是:

@XmlElement(name = "AppInstllCd") 
    protected String AppInstllCd; 
    @XmlElement(name = "AppVrsn") 
    protected String AppVrsn; 
    @XmlElement(name = "OS") 
    protected String OS; 
    @XmlElement(name = "OSVrsn") 
    protected String OSVrsn; 
    @XmlElement(name = "Device") 
    protected String Device; 

回答

0

您需要避開JAXB使用的命名約定。有些選擇,我沒有親自嘗試自己會遵循接受的答案或this SO question頂部投票答案。


另一種選擇是創建一個XJC插件。雖然這可能是矯枉過正。 (我目前正在開發一個插件XJC所以我有偏見。)

Tutorial that helped me getting started with XJC Plugins.

該插件的代碼可以做一些事情,幾乎是這樣的:

import com.sun.tools.xjc.Plugin; 

public class XJCPlugin extends Plugin { 
    @Override 
    public String getOptionName() { //... } 

    @Override 
    public int parseArgument(Options opt, String[] args, int i) throws BadCommandLineException, IOException { 
     return 1; 
    } 

    @Override 
    public boolean run(Outline model, Options opt, ErrorHandler errorHandler) throws SAXException { 
     //... 
    } 
    @Override 
    public void postProcessModel(Model model, ErrorHandler errorHandler) { 
     //This method changes the attribute names 
     for (CClassInfo classInfo : model.beans().values()) //for each class 
      for (CPropertyInfo propertyInfo : classInfo.getProperties()) //for each attribute 
       propertyInfo.setName(false, Utility_StringHandling.firstCharacterToUpperCase(propertyInfo.getName(false))); 
    } 
    static String firstCharacterToUpperCase(String input) { 
     char c[] = input.toCharArray(); 
     c[0] = Character.toUpperCase(c[0]); 
     return new String(c); 
    } 
}