Reputation: 73
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?
Upvotes: 1
Views: 1365
Reputation: 1
Use http://www.java2s.com/Code/Jar/x/xpp3.htm for parsing. xstream automaticaly detects it. Then one Line: Vector g = (Vector) xstream.fromXML(String xml );
Upvotes: 0
Reputation: 73
I was able to read the file in using the following code:
public class Reader {
public static void main(String[] args) {
XStream xstream = new XStream(new DomDriver());
try {
FileInputStream fis = new FileInputStream("c:/dictionary.xml");
ObjectInputStream in = xstream.createObjectInputStream(fis);
xstream.alias("word", Word.class);
Word a = (Word)in.readObject();
Word b = (Word)in.readObject();
in.close();
System.out.println(a.toString());
System.out.println(b.toString());
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Now, instead of saying:
Word a = (Word)in.readObject();
Word b = (Word)in.readObject();
I'd like to read the objects into a vector by using a loop. My only problem now is how can I know how many objects are in the ObjectInputStream. It doesn't appear to have a method that tells me the number of objects or the size...
Upvotes: 1