首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XStream映射集合

XStream映射集合
EN

Stack Overflow用户
提问于 2011-04-02 02:24:47
回答 1查看 5.5K关注 0票数 1

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

代码语言:javascript
复制
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是

代码语言:javascript
复制
<?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>

我的映射是

代码语言:javascript
复制
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。它无法映射子数组列表。我不知道虫子在哪里。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2013-01-17 16:32:10

试试这个..。

我想这一定是一种...

代码语言:javascript
复制
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>";
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5517286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档