【问题标题】:Read XML file into vector of objects using XStream使用 XStream 将 XML 文件读入对象向量
【发布时间】:2012-03-19 06:20:16
【问题描述】:

我想将 XML 文件用作我正在处理的 NLP 项目的字典。我目前有一个“Words”类,它是“Word”对象的向量。

public class Words {

private Vector<Word> vect;

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

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

“Word”类看起来像这样:

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 文件读回对象向量?

【问题讨论】:

    标签: xml vector xstream xml-deserialization


    【解决方案1】:

    我能够使用以下代码读取文件:

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

    }

    现在,而不是说:

    Word a = (Word)in.readObject();
    Word b = (Word)in.readObject();
    

    我想使用循环将对象读入向量。我现在唯一的问题是如何知道 ObjectInputStream 中有多少对象。它似乎没有告诉我对象数量或大小的方法......

    【讨论】:

      【解决方案2】:

      使用http://www.java2s.com/Code/Jar/x/xpp3.htm 进行解析。 xstream 会自动检测到它。 然后一行: 向量 g = (向量) xstream.fromXML(String xml);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-19
        • 2011-03-09
        • 2016-03-14
        • 2014-04-21
        • 2013-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多