Java XStream映射集合

Java XStream映射集合,java,xstream,Java,Xstream,我尝试使用XStream将xml映射到java对象。但这里有一个bug public class Concepts { List<Concept> conceptList = new ArrayList<Concept>(); } public class Concept { String id; String name; String table; List<Child> childList= new ArrayL

我尝试使用XStream将xml映射到java对象。但这里有一个bug

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;
}
最后一行有错误。它无法映射子arraylist。我不知道臭虫在哪里

谢谢。

试试这个

我想这一定是一种方式

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>";
}
公共类测试{
公共类概念{
List conceptList=新的ArrayList();
}
公共阶级概念{
字符串id;
字符串名;
字符串表;
儿童名单儿童名单;
}
公共类儿童名单{
List childList=new ArrayList();
}
公营儿童{
字符串id;
字符串名;
字符串子表;
字符串rel;
}
公共静态void main(字符串[]args){
XStream XStream=新的XStream();
别名(“概念”,concepts.class);
别名(“concept”,concept.class);
别名(“ChildList”,ChildList.class);
别名(“child”,child.class);
别名属性(Concept.class,“id”,“id”);
别名属性(Concept.class,“name”,“name”);
别名属性(Concept.class,“table”,“table”);
别名属性(Child.class,“id”,“id”);
别名属性(Child.class,“name”,“name”);
别名属性(Child.class,“childTable”,“childTable”);
别名属性(Child.class,“rel”,“rel”);
addImplicitCollection(ChildList.class,“ChildList”);
addImplicitCollection(Concepts.class,“conceptList”);
对象fromXML=xstream.fromXML(getmess());
for(概念字符串:((概念)fromXML).conceptList){
System.out.println(string.id+“”+string.name+“”+string.table);
if(string.childList!=null){
for(子字符串1:string.childList.childList){
if(string1!=null){
System.out.println(“+string1.id+”“+string1.name+”“+string1.childTable+”“+string1.rel”);
}
}
}
}
}
静态字符串getmess(){
返回“”
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ ""
+ "";
}

}

在序列化、反序列化或两者同时进行时,您是否会遇到此问题?最后一行似乎有输入错误(第二个
childList
不应该是
child
?),但除此之外,您能否向我们提供stacktrace或其他信息?线程“main”中的错误消息异常com.thoughtworks.xstream.converters.ConversionException:com.test.model.child类型的元素子元素未定义为com.test.model.child类型中的字段您是否解决过此问题?请解释您的问题,添加错误消息,并注意您的尝试。
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
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>";
}