XStream映射一个集合

发布于 2024-10-28 18:11:01 字数 1634 浏览 7 评论 0原文

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

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

最后一行有错误。它无法映射子数组列表。我不知道 bug 出在哪里。

谢谢。

I try to use XStream to map xml to java object. But here is one 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;
}

the xml is

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

my mapping is

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

The last line has bug. it can't map the child arraylist. I don't konw where is the bug.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘羡 2024-11-04 18:11:01

尝试一下...

我想这一定是一种方法...

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

}

Try with this...

I thought this must be a one of the way...

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文