最近学习了xml和java bean对象间的相互转换,感觉可以和大家分享一下,也希望和大家以后多多交流:
需要引用的包:xstream-1.3.1.jar
主要应用XStream的fromXML()和toXML()方法来实现xml和bean间的相互转换,后面附有源码,下面问题都通过讨论源码展开
碰到的问题1:
Exception in thread "main" java.lang.IllegalArgumentException: XPP3 pull parser library not present. Specify another driver. For example: new XStream(new DomDriver())
at com.thoughtworks.xstream.io.xml.XppDriver.loadLibrary(XppDriver.java:62)
at com.thoughtworks.xstream.io.xml.XppDriver.createReader(XppDriver.java:43)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845)
at org.evon.example.XmlJava.main(XmlJava.java:22)
解决办法:
XStream对象初始化时,调用构造方法XStream(HierarchicalStreamDriver hierarchicalStreamDriver),具体用法如下:
XStream xStream = new XStream(new DomDriver());
碰到的问题2:
<org.evon.example.User>
<uid>8888</uid>
<uname>evon</uname>
<type>
<id>type</id>
<org.evon.example.Point>
<x>100</x>
<y>100</y>
</org.evon.example.Point>
<org.evon.example.Point>
<x>200</x>
<y>200</y>
</org.evon.example.Point>
</type>
<point>
<x>0</x>
<y>0</y>
</point>
</org.evon.example.User>
</org.evon.example.User>
解决办法:
xStream.alias("user",User.class);
xStream.alias("type",Type.class);
xStream.alias("point",Point.class);
解决办法:
方法1:调用addImplicitCollection方法,具体使用如下
xStream.addImplicitCollection(Type.class,"pointList");
方法2:使用注解,需要进行两步操作,具体使用如下
1)Type类pointList属性声明前加上注解@XStreamImplicit(itemFieldName="point")
@XStreamImplicit(itemFieldName="point")
private List<Point> pointList;
2)解析前开启注解解析
xStream.autodetectAnnotations(true);
源码如下:
package org.evon.example; import java.util.ArrayList; import java.util.List; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamImplicit; import com.thoughtworks.xstream.io.xml.DomDriver; public class XmlJava { public static void main(String[] args) { User user = createUser(); XStream xStream = new XStream(new DomDriver()); xStream.addImplicitCollection(Type.class,"pointList"); // xStream.autodetectAnnotations(true); xStream.alias("user",User.class); xStream.alias("type",Type.class); xStream.alias("point",Point.class); String xml = xStream.toXML(user); System.out.println("bean转换成的xml:"); System.out.println(xml); User newUser = (User)xStream.fromXML(xml); System.out.println("xml转换成的bean,bean重新转换成的xml:"); System.out.println(xStream.toXML(newUser)); } private static User createUser(){ Type type = new Type(); type.setId("type"); List<Point> pointList = new ArrayList<Point>(); Point point1 = new Point(); point1.setX(100); point1.setY(100); Point point2 = new Point(); point2.setX(200); point2.setY(200); pointList.add(point1); pointList.add(point2); type.setPointList(pointList); Point point = new Point(); point.setX(0); point.setY(0); User user = new User(); user.setUid(8888); user.setUname("evon"); user.setType(type); user.setPoint(point); return user; } } class User { private int uid; private String uname; private Type type; private Point point; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public Type getType() { return type; } public void setType(Type type) { this.type = type; } public Point getPoint() { return point; } public void setPoint(Point point) { this.point = point; } } class Type{ private String id; // @XStreamImplicit(itemFieldName="point") private List<Point> pointList; public String getId() { return id; } public void setId(String id) { this.id = id; } public List<Point> getPointList() { return pointList; } public void setPointList(List<Point> pointList) { this.pointList = pointList; } } class Point{ private int x; private int y; public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } }
由于本人所学有限,有的地方论述不尽详细;若有问题,欢迎大家指正,QQ:823746747