2011-04-01 51 views
1

我嘗試使用XStream把XML映射到Java對象。但這裏有一個錯誤。XStream的地圖集合

public class Concepts { 
    List<Concept> conceptList = new ArrayList<Concept>(); 

} 
public class Concept { 
    String id; 
    String name; 
    String table; 

    List<Child> childList= new ArrayList<Child>(); 

} 
public class Child { 
    String id; 
    String name; 
    String childTable; 
    String rel; 
} 

的XML是

<?xml version="1.0" encoding="UTF-8"?> 
<concepts> 
    <concept id="1234" name="student" table="student">  
     <childList> 
      <child id="" name="Address" childTable="address" rel="one-to-one"/>   
      <child id="" name="Score" childTable="score" rel="one-to-many"/>    
     </childList> 
    </concept> 

    <concept id="12343" name="address" table="address"/>  
    <concept id="123433" name="store" table="score"/> 
</concepts> 

我的映射是

xstream.alias("concepts", Concepts.class); 
xstream.alias("concept", Concept.class); 
xstream.alias("child", Child.class); 

xstream.useAttributeFor("name", String.class); 
xstream.useAttributeFor("id", String.class); 
xstream.useAttributeFor("table", String.class); 
xstream.useAttributeFor("childTable", String.class); 
xstream.useAttributeFor("rel", String.class); 

// collection 
xstream.addImplicitCollection(Concepts.class, "conceptList","concept", Concept.class); 
xstream.addImplicitCollection(Concept.class, "childList", "childList", Child.class); //bug 

最後一行有錯誤。它無法映射子列表列表。我不知道錯誤在哪裏。

謝謝。

+0

不要序列化時,反序列化你遇到這個問題,或者兩者? – mre 2011-04-01 19:08:27

+0

你似乎在最後一行有個拼寫錯誤(第二個'childList'不應該是'child'?),但除此之外,你能提供給我們一個堆棧跟蹤還是什麼? – 2011-04-01 19:22:47

+0

錯誤消息線程「main」中的異常com.thoughtworks.xstream.converters.ConversionException:類型爲com.test.model.Child的元素子類未定義爲com.test.model.Child類型中的字段 – user534009 2011-04-04 13:57:09

回答

0

這種嘗試......

我想這一定是的方式一個...

public class test { 

public class Concepts { 

    List<Concept> conceptList = new ArrayList<Concept>(); 
} 

public class Concept { 

    String id; 
    String name; 
    String table; 
    ChildList childList; 
} 

public class ChildList { 

    List<Child> childList = new ArrayList<Child>(); 
} 

public class Child { 

    String id; 
    String name; 
    String childTable; 
    String rel; 
} 

public static void main(String[] args) { 
    XStream xstream = new XStream(); 
    xstream.alias("concepts", Concepts.class); 
    xstream.alias("concept", Concept.class); 
    xstream.alias("ChildList", ChildList.class); 
    xstream.alias("child", Child.class); 
    xstream.aliasAttribute(Concept.class, "id", "id"); 
    xstream.aliasAttribute(Concept.class, "name", "name"); 
    xstream.aliasAttribute(Concept.class, "table", "table"); 
    xstream.aliasAttribute(Child.class, "id", "id"); 
    xstream.aliasAttribute(Child.class, "name", "name"); 
    xstream.aliasAttribute(Child.class, "childTable", "childTable"); 
    xstream.aliasAttribute(Child.class, "rel", "rel"); 
    xstream.addImplicitCollection(ChildList.class, "childList"); 
    xstream.addImplicitCollection(Concepts.class, "conceptList"); 

    Object fromXML = xstream.fromXML(getmess()); 
    for (Concept string : ((Concepts) fromXML).conceptList) { 
     System.out.println(string.id + " " + string.name + " " + string.table); 
     if (string.childList != null) { 
      for (Child string1 : string.childList.childList) { 
       if (string1 != null) { 
        System.out.println("  " + string1.id + " " + string1.name + " " + string1.childTable + "  " + string1.rel); 
       } 
      } 
     } 
    } 
} 

static String getmess() { 
    return "<concepts>" 
      + "<concept id=\"1234\" name=\"student\" table=\"student\">" 
      + "<childList>" 
      + "<child id=\"1\" name=\"Address\" childTable=\"address\" rel=\"one-to-one\"/>" 
      + "<child id=\"2\" name=\"Score\" childTable=\"score\" rel=\"one-to-many\"/>" 
      + "</childList>" 
      + "</concept>" 
      + "<concept id=\"12343\" name=\"address\" table=\"address\"/>" 
      + "<concept id=\"123433\" name=\"store\" table=\"score\"/>" 
      + "</concepts>"; 
} 

}