XStream映射一个集合
我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一下...
我想这一定是一种方法...
}
Try with this...
I thought this must be a one of the way...
}