您genearlly不使用對JAXB對象的列表域制定者。
相反,您使用getter作爲列表和maniuplate返回的列表。
例JAXB對象:
class JaxbExample {
@XmlElement(name="stringList", required = true)
private List<String> stringList;
public List<String> getStringList() {
return stringList;
}
}
添加三個字符串到stringList
:
JaxbExample someObject = ...;
// add three Strings to stringList
someObject.getStringList().add("foo");
someObject.getStringList().add("bar");
someObject.getStringList().add("baz");
// now the list contains 3 new strings and there was
// no need to use a setter.
組的StringList到現有的列表:
JaxbExample someObject = ...;
List<String> someList = ...;
// set someObject's stringList to someList
someObject.getStringList().addAll(someList);
進一步闡明...
我們有時使用XJC utility從XML模式文件(.XSD文件)生成我們的JAXB Java類。
當生成的類包含List元素時,不會爲List生成setter方法。
以下注釋出現在上面的吸氣器爲每個List:
/**
* Gets the value of the stringList property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the stringList property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getStringList().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
*
*
*/
希望這評論做了解釋比我有更好的工作!
該setter不使用,因爲沒有必要。您可以簡單地操縱setter返回的列表。看到下面的答案... – jahroy 2013-02-20 20:12:32
@jahroy我回滾你的編輯,因爲我覺得你改變了太多的問題。我不想知道列表的具體工作方式 - 我想知道一般情況下爲什麼我的方法無效。 – chama 2013-02-20 20:49:21
什麼不適合你的方法?我不是在建議一種替代方法,我只是試圖描述如何在使用JAXB生成的對象中使用列表。目前還不清楚你在問什麼。當然你的方法名爲「_setMyObjects_」沒有被調用。它不是你的列表的setter:它將一個數組作爲參數而不是一個列表!我試圖做的一點是JAXB不使用列表的setters。我的JAXB生成的對象甚至沒有列表字段的setter。 – jahroy 2013-02-20 21:18:20