2011-05-19 64 views
1

我試圖去下面的XML到使用XStream的Java對象序列化。如何使用XStream反序列化這個xml?

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

誰能告訴我什麼是錯誤的代碼?

+0

聞起來像類路徑或類加載器地獄......請雙精度和三重檢查,如果你的'Corpus'類是在classpath當你運行應用程序 – 2011-05-19 06:23:18

+0

@Andreas_D,我已經包括西河和XPP jar文件在我的類路徑。還有什麼我需要添加到類路徑中? – Shekhar 2011-05-19 06:27:24

+0

@Shekhar - 錯誤信息導致的想法,那'com.srl.SSAOutputModel.Corpus'沒有找到 - 無論是不是在類路徑或通過不同的類加載器加載...是一些服務器的獨立應用程序或部分/客戶端架構? – 2011-05-19 06:31:40

回答

3

這個答案總結我們已經張貼在評論部分的解決方案。

這是有問題的行導致錯誤:

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

xml-tags are case sensitive和源文檔中的標籤名是<s>而別名爲<S>定義。

更改該行

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

顯然解決了這個問題:)