對於以下示例XSD片段:JAXB xjc:如何爲字符串生成代碼,如果值爲空則返回空值?
< xs:attribute name="SEGMENT" default="" use="optional" type="xs:string"/ >
時XJC生成包含SEGMENT
豆屬性類,下面的吸氣劑是自動生成的:
public String getSEGMENT() {
if (segment == null) {
return "";
} else {
return segment;
}
}
我的問題是你如何得到它對xs:element
對象做同樣的事情嗎?換句話說,鑑於以下XSD片段:
< xs:element name="NAME" default="" type="xs:string"/ >
我想知道如果我能得到XJC生成以下:
public String getNAME() {
if (name == null) {
return "";
} else {
return name;
}
}
如何才能做到這一點?因爲它爲默認值的屬性
一個字段初始肯定是合適的。現在的問題是如何讓xjc爲每個類的每個字符串添加空字符串初始值設定項?也許在一個單獨的綁定文件? –
@java luva:我在回答中添加了更多細節,並且發現了一些看起來很有趣的插件。看看它是否有幫助。 – Bogdan
感謝您的額外信息。我有一個問題,但。您發送的鏈接涉及設置maven或ant任務。我不使用maven。而設置螞蟻任務時的問題是我得到一個鏈接錯誤,指出不同的類將被加載相同的名稱。我相信問題是java 1.6內置了xjc,並且通過將jaxb-xjc-2.1.9.jar添加到我的xjc taskdef類路徑中,它們發生衝突。我的問題是如何讓ant腳本忽略1.6(我的JAVA_HOME設置爲)並使用我的taskdef類路徑,或者在taskdef中引用1.6 xjc jar? –