我想將XML文件用作我正在處理的NLP項目的字典。我目前有一個「Words」類,它是「Word」對象的向量。使用XStream將XML文件讀入對象矢量
public class Words {
private Vector<Word> vect;
public Words(){
vect = new Vector<Word>();
}
public void add(Word w){
vect.add(w);
}
的 「字」 類看起來是這樣的:
public class Word {
private String name;
private String partOfSpeech;
private String category;
private String definition;
}
我已成功通過使用此代碼寫的 「字」 矢量使用XStream的XML:
public class Writer {
public static void main(String[] args) {
XStream xstream = new XStream();
xstream.alias("words", Words.class);
xstream.alias("word", Word.class);
xstream.addImplicitCollection(Words.class, "vect");
Words vect = new Words();
vect.add(new Word("dog", "noun", "animal", "a domesticated canid, Canis familiaris, bred in many varieties"));
vect.add(new Word("cat", "noun", "animal", "a small domesticated carnivore, Felis domestica or F. catus, bred in a number of varieties"));
try {
FileOutputStream fs = new FileOutputStream("c:/dictionary.xml");
xstream.toXML(vect, fs);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
這一切似乎工作正常,並給我以下XML文件:
<words>
<word>
<name>dog</name>
<partOfSpeech>noun</partOfSpeech>
<category>animal</category>
<definition>a domesticated canid, Canis familiaris, bred in many varieties</definition>
</word>
<word>
<name>cat</name>
<partOfSpeech>noun</partOfSpeech>
<category>animail</category>
<definition>a small domesticated carnivore, Felis domestica or F. catus, bred in a number of varieties</definition>
</word>
</words>
我的問題是如何使用XStream將此XML文件讀回到對象的矢量中?