如何使用XStream反序列化这个xml?

时间:2011-05-19 06:16:59

标签: java xml-serialization xstream xml-deserialization

我正在尝试使用XStream将xml序列化为Java对象。

<?xml version="1.0" encoding="UTF-8"?>
<corpus>
<s id="1">
  <tree style="penn">
(ROOT
  (S
    (NP (NNP Brooklyn) (NN man))
    (VP (VBD accused)
      (PP (IN of)
        (NP (NN forgery))))
    (. .)))
  </tree>
<dependencies style="typed">
  <dep type="nn">
    <governor idx="2">man</governor>
    <dependent idx="1">Brooklyn</dependent>
  </dep>
  <dep type="nsubj">
    <governor idx="3">accused</governor>
    <dependent idx="2">man</dependent>
  </dep>
  <dep type="prep_of">
    <governor idx="3">accused</governor>
    <dependent idx="5">forgery</dependent>
  </dep>
</dependencies>
</s>

<s id="2">
  <tree style="penn">
(ROOT
  (S
    (S
      (NP (DT A)
        (ADJP (CD 36) (NN year) (JJ old))
        (NNP Brooklyn) (NN man))
      (VP (VBZ is)
        (VP (VBG facing)
          (NP (JJ several) (NN felony) (NNS charges))
          (SBAR (IN after)
            (S
              (NP (PRP he))
              (VP (VBD was)
                (VP (VBN accused)
                  (PP (IN of)
                    (S
                      (VP (VBG giving)
                        (NP
                          (NP (DT a) (JJ forged) (NN prescription))
                          (PP (IN for)
                            (NP (NN oxycodone))))
                        (PP (TO to)
                          (NP (DT a) (JJ local) (NN pharmacist)))))))))))))
    (, ,)
    (NP (NNP Endicott) (NNS police))
    (VP (VBD said))
    (. .)))
  </tree>
<dependencies style="typed">
  <dep type="det">
    <governor idx="6">man</governor>
    <dependent idx="1">A</dependent>
  </dep>
</s>
</corpus>

我为这个xml结构创建了以下类:

public class Corpus {

    private List<Sentence> sentences = new ArrayList<Sentence>();

    public List<Sentence> getSentences() {
        return sentences;
    }

    public void setSentences(List<Sentence> sentences) {
        this.sentences = sentences;
    }

    public void add(Sentence sentence) {
        this.sentences.add(sentence);
    }
}

public class Sentence {

    private int id;
    private Tree tree;
    private Dependencies dependencies;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Tree getTree() {
        return tree;
    }
    public void setTree(Tree tree) {
        this.tree = tree;
    }

    public Dependencies getDependencies() {
        return dependencies;
    }

    public void setDependencies(Dependencies dependencies) {
        this.dependencies = dependencies;
    }

    public Sentence(int id) {
        this.id = id;
    }
}

我还为每个元素都有其他类,但我没有在这里提到过这些类。当我运行项目时,我收到以下错误:

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: s : s : s : s
---- Debugging information ----
message             : s : s
cause-exception     : com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : s : s
class               : com.srl.SSAOutputModel.Corpus
required-type       : com.srl.SSAOutputModel.Corpus
path                : /corpus/s
line number         : 1

以下代码尝试反序列化xml

    xstream.alias("corpus", Corpus.class);
    xstream.addImplicitCollection(Corpus.class, "sentences");

    xstream.useAttributeFor(Sentence.class, "id");
    xstream.aliasField("id", Sentence.class, "id");

    xstream.alias("S", Sentence.class);

    xstream.useAttributeFor(Tree.class, "style");
    xstream.aliasField("style",Tree.class, "style");

    xstream.omitField(Tree.class, "content");
    xstream.autodetectAnnotations(true);

    Corpus cx = (Corpus) xstream.fromXML(lines);

有人可以告诉我代码有什么问题吗?

1 个答案:

答案 0 :(得分:3)

这个答案总结了我们已经在评论部分发布的解决方案。

这是导致错误的违规行:

xstream.alias("S", Sentence.class);

xml-tags are case sensitive,源文档中的代码名称为<s>,而别名是为<S>定义的。

将此行更改为

xstream.alias("s", Sentence.class);

显然解决了这个问题:)

相关问题