I'd like to use an XML file as a dictionary for an NLP project I'm working
on. I currently have a "Words" class which is a vector of "Word" objects.

public class Words {

private Vector<Word> vect;

public Words(){
    vect = new Vector<Word>();

}

public void add(Word w){
    vect.add(w);
}

The "Word" class looks something like this:

public class Word {

private String name;
private String partOfSpeech;
private String category;
private String definition;
}

I have managed to write the "Words" vector to XML using XStream by using
this code:

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

}
}

This all seems to work fine and gives me the following XML file:

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

My question is how do I use XStream to read this XML file back into a
vector of objects?  Is there an example similar to this you could point me
to?

Reply via email to