gpt4 book ai didi

java - xjc java 类生成,其中字段与 @XmlElement 具有相同的名称

转载 作者:行者123 更新时间:2023-12-02 13:29:52 24 4
gpt4 key购买 nike

我使用此 xjc 从 .xsd 文件创建 java 类脚本

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

我想知道是否可以以某种方式更改我的脚本,这使得我的类字段具有与 @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;

最佳答案

您需要绕过 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);
}
}

关于java - xjc java 类生成,其中字段与 @XmlElement 具有相同的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43233629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com