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