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

2 篇文章 0 订阅
今天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;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值