繁体   English   中英

如何使用XStream反序列化此xml?

[英]How to deserialize this xml using XStream?

我正在尝试使用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);

谁能告诉我代码有什么问题吗?

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

这是导致错误的冒犯行:

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

xml-tags区分大小写 ,源文档中的标签名称为<s>而别名是为<S>定义的。

将此行更改为

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

显然解决了问题:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM