加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:23:34 所属栏目:百科 来源:网络整理
导读:今天x被stream对xmlnode的属性(attribute)解析的问题一直困扰着,查询了很久都告知我要手写一个Converter,那岂不意味着我每解析一个xml文件,就得写一次Converter,那样太脑残了,最后搜索到其实可以用注解解决这个问题 XStream常用注解 用法 @XStreamOmitFi
今天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;
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读