xml与bean间相互转换(补充)

前端之家收集整理的这篇文章主要介绍了xml与bean间相互转换(补充)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
今天x被stream对xmlnode的属性(attribute)解析的问题一直困扰着,查询了很久都告知我要手写一个Converter,那岂不意味着我每解析一个xml文件,就得写一次Converter,那样太脑残了,最后搜索到其实可以用注解解决这个问题
XStream常用注解 用法
@XStreamOmitField 指定该属性为一个节点
@XStreamAsAttribute 指定该属性为一个节点的一个属性
@XStreamAlias("alias") 指定该属性在xml文件中对应的节点或属性名称为alias

@XStreamImplicit(itemFieldName="alias") 指定同一标签下多个同名元素的显示为alias


待解析的xml字串:

<user uname="evon">
<utype tid="type">
<tpoint>
<ux>100</ux>
<uy>100</uy>
</tpoint>
<tpoint>
<ux>200</ux>
<uy>200</uy>
</tpoint>
</utype>
<upoint>
<ux>0</ux>
<uy>0</uy>
</upoint>
</user>


用XStream解析,节点属性及子节点均使用注解,特别注意的是:在使用注解的时候一定要在使用前开启注解解析

    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.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XmlBean {
    public static void main(String[] args) { 
        User user = createUser();
        XStream xStream = new XStream(new DomDriver());
        xStream.autodetectAnnotations(true);
        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;
    }
}

@XStreamAlias("user")
class User {
	@XStreamOmitField
	@XStreamAlias("uid")
	private int uid;
	@XStreamAsAttribute
	@XStreamAlias("uname")
	private String uname;
	@XStreamAlias("utype")
	private Type type;
	@XStreamAlias("upoint")
	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;
	}
}

@XStreamAlias("utype")
class Type{
	@XStreamAsAttribute
	@XStreamAlias("tid")
	private String id;
	@XStreamImplicit(itemFieldName="tpoint")
	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;
	}
}

@XStreamAlias("upoint")
class Point{
	@XStreamAlias("ux")
	private int x;
	@XStreamAlias("uy")
	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;
	}
}
原文链接:https://www.f2er.com/xml/299769.html

猜你在找的XML相关文章