开发者

How to deserialize this xml using XStream?

开发者 https://www.devze.com 2023-03-07 02:57 出处:网络
I am trying to de serialize following xml into Java object using XStream. <?xml version=\"1.0\" encoding=\"UTF-8\"?>

I am trying to de serialize following xml into Java object using XStream.

<?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&l开发者_如何学Got;/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>

I have created following classes for this xml structure :

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

and

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

I have other classes also for each element but I have not mentioned those classes here. When I run the project, I get following error :

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

Following code tries to deserialize the 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);

Can anyone please tell me what is wrong with the code?


This answer summarizes the solution we already posted in the comment section.

This is the offending line that cause the error:

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

xml-tags are case sensitive and the tag name in the source document is <s> while the alias is defined for <S>.

Changing this line to

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

obviously solved the problem :)

0

精彩评论

暂无评论...
验证码 换一张
取 消