码迷,mamicode.com
首页 > 其他好文 > 详细

xstream 实现xml和对象转换

时间:2016-09-24 19:09:28      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

1.jar包 xstream-1.4.2.jar     xpp3_min-1.1.4c.jar

 

2.工具类

 

package com.core.util;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.core.model.Mobile;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.ClassAliasingMapper;

@SuppressWarnings("all")
public class XmlBeanUtil {

	private XmlBeanUtil() {}
	/**
	 * 实体转化xml 
	 * @param object 
	 * @param alias
	 * @return
	 */
	public static String beanToXml(Object object, Map<String, Class> aliasMap) {
		XStream xstream = new XStream(new DomDriver());
		String name = null;
		Class clazz = null;
		for (Map.Entry<String, Class> entry : aliasMap.entrySet()) {
			name = entry.getKey();
			clazz = entry.getValue();
			xstream.aliasType(name, clazz);
		}
		return xstream.toXML(object);
	}
	
	
   /**
    * xml 转换成实体bean
    * @param xml
    * @param aliasMap
    * @return
    */
    public static Object xmlToBean(String xml, Map<String,Class> aliasMap){
    	XStream xstream = new XStream(new DomDriver());
		String name = null;
		Class clazz = null;
		for (Map.Entry<String, Class> entry : aliasMap.entrySet()) {
			name = entry.getKey();
			clazz = entry.getValue();
			xstream.aliasType(name, clazz);
		}
        return xstream.fromXML(xml);
    }
   
}

  3.demo

 

package com.core.model;

import java.io.Serializable;
import java.util.List;

public class Mobile implements Serializable{

	private static final long serialVersionUID = -3457855554027518915L;
	private String type;
	private String productAddr;
	private List<PhoneBo> phones ;
	private List<String> colors;
	private List<Integer> prices;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getProductAddr() {
		return productAddr;
	}
	public void setProductAddr(String productAddr) {
		this.productAddr = productAddr;
	}
	public List<PhoneBo> getPhones() {
		return phones;
	}
	public void setPhones(List<PhoneBo> phones) {
		this.phones = phones;
	}
	public List<String> getColors() {
		return colors;
	}
	public void setColors(List<String> colors) {
		this.colors = colors;
	}
	public List<Integer> getPrices() {
		return prices;
	}
	public void setPrices(List<Integer> prices) {
		this.prices = prices;
	}
	@Override
	public String toString() {
		return "Mobile [type=" + type + ", productAddr=" + productAddr
				+ ", phones=" + phones + ", colors=" + colors + ", prices="
				+ prices + "]";
	}
	
}

  

package com.core.model;

public class PhoneBo {

	private int id;
	
	private String name;
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public String toString() {
		return "QueBo [id=" + id + ", name=" + name + "]";
	}
}

  

package com.core.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.core.model.Mobile;
import com.core.model.PhoneBo;
import com.core.util.XmlBeanUtil;
public class TestXmlBeanUtil {

	public static void main(String[] args) {
		test1();
	}
	public static void test1(){
		Mobile mobile = new Mobile();
		
		PhoneBo phone = new PhoneBo();
		phone.setId(100001);
		phone.setName("iphone6");
		
		List<PhoneBo> phones = new ArrayList<PhoneBo>();
		phones.add(phone);
		
		List<String> colors = new ArrayList<String>();
		colors.add("高雅红");
		colors.add("活力橙");
		colors.add("动力黄");
		colors.add("清新绿");
		colors.add("经典蓝");
		colors.add("玫瑰金");
		colors.add("诱惑紫");
		
		mobile.setPhones(phones);
		mobile.setType("iso");
		mobile.setProductAddr("郑州富士康");
		mobile.setColors(colors);
		
		List<Integer>prices = new ArrayList<Integer>();
		prices.add(11);
		prices.add(11);
		prices.add(11);
		prices.add(11);
		mobile.setPrices(prices);
		
		Map<String, Class> map = new HashMap<String, Class>(){
			{
				put("data", Mobile.class);
				put("phones", List.class);
				put("phone", PhoneBo.class);
//				put("colors", List.class);
//				put("prices", List.class);
//				put("price", int.class);
			}
		};
		
//		String xml = XmlBeanUtil.beanToXml(mobile, map,"prices",int.class, "price");
//		System.out.println(xml);
		String xmlData = XmlBeanUtil.beanToXml(mobile, map);
//		Mobile m = (Mobile) XmlBeanUtil.xmlToBean(xml, map,"prices",int.class, "price");
		System.out.println(xmlData);
		Mobile m = (Mobile) XmlBeanUtil.xmlToBean(xmlData, map);
		System.out.println(m.toString());
	}
}

  

4.测试结果

 

<data>
  <type>iso</type>
  <productAddr>郑州富士康</productAddr>
  <phones>
    <phone>
      <id>100001</id>
      <name>iphone6</name>
    </phone>
  </phones>
  <colors>
    <string>高雅红</string>
    <string>活力橙</string>
    <string>动力黄</string>
    <string>清新绿</string>
    <string>经典蓝</string>
    <string>玫瑰金</string>
    <string>诱惑紫</string>
  </colors>
  <prices>
    <int>11</int>
    <int>11</int>
    <int>11</int>
    <int>11</int>
  </prices>
</data>
Mobile [type=iso, productAddr=郑州富士康, phones=[QueBo [id=100001, name=iphone6]], colors=[高雅红, 活力橙, 动力黄, 清新绿, 经典蓝, 玫瑰金, 诱惑紫], prices=[11, 11, 11, 11]]

  

  

xstream 实现xml和对象转换

标签:

原文地址:http://www.cnblogs.com/shell-blog/p/5903791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!