XStream xml2object 和 object2xml

首先我们把xstream-distribution-1.4.4-bin里面的jar加到编译和运行环境里。
然后,我们定义一个pojo类。注意它不需要implements java.io.Serializable

public class SexyWoman {
	private String name;
	private long high;
	private long weight;
	public static String face="pretty";
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public long getHigh() {
		return high;
	}
	public void setHigh(long high) {
		this.high = high;
	}
	public long getWeight() {
		return weight;
	}
	public void setWeight(long weight) {
		this.weight = weight;
	}
	
}



然后我们去写运行测试类。
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class XStreamTest {
	public static void main(String[] args) {
		SexyWoman sw1=new SexyWoman();
		sw1.setName("maggie");
		sw1.setHigh(172);
		sw1.setWeight(65);
		sw1.face="ddd";
		String xmlContent=doPojo2Xml(sw1);
		System.out.println(xmlContent);
		System.out.println("\n\n\n");
		SexyWoman sw2=new SexyWoman();
		Object obj=doXml2Pojo(xmlContent,sw2);
		System.out.println(((SexyWoman)obj).getName());
	}

	public static String doPojo2Xml(Object obj){
		XStream xstream=new XStream(new DomDriver());
		xstream.alias(obj.getClass().getSimpleName(), obj.getClass());
		String xmlContent=xstream.toXML(obj);
		return xmlContent;
	}
	
	public static Object doXml2Pojo(String xmlContent,Object obj){
		XStream xstream=new XStream(new DomDriver());
		xstream.alias(obj.getClass().getSimpleName(), obj.getClass());
		Object getObj=xstream.fromXML(xmlContent);
		return getObj;
	}
}


最后输出

<SexyWoman>
  <name>maggie</name>
  <high>172</high>
  <weight>65</weight>
</SexyWoman>




maggie


这里可以发现 static 属性并没有被进入xml中。

这里我们要注意的是String最大长度是65xxx, 所以我们不能用太大的类和xml.否则会出问题。当然Xstream开源里有另外的api专门针对这种问题。



如果我们给她加辆车呢
public class Car implements java.io.Serializable{
	private String cardName;

	public String getCardName() {
		return cardName;
	}

	public void setCardName(String cardName) {
		this.cardName = cardName;
	}
}
public class XStreamTest {
	public static void main(String[] args) {
		SexyWoman sw1=new SexyWoman();
		sw1.setName("maggie");
		sw1.setHigh(172);
		sw1.setWeight(65);
		sw1.face="ddd";
		Car car1=new Car();
		car1.setCardName("ppo");
		sw1.setCar(car1);
		String xmlContent=doPojo2Xml(sw1);
		System.out.println(xmlContent);
		System.out.println("\n\n\n");
		SexyWoman sw2=new SexyWoman();
		Object obj=doXml2Pojo(xmlContent,sw2);
		System.out.println(((SexyWoman)obj).getName());
	}

	public static String doPojo2Xml(Object obj){
		XStream xstream=new XStream(new DomDriver());
		xstream.alias(obj.getClass().getSimpleName(), obj.getClass());
		String xmlContent=xstream.toXML(obj);
		return xmlContent;
	}
	
	public static Object doXml2Pojo(String xmlContent,Object obj){
		XStream xstream=new XStream(new DomDriver());
		xstream.alias(obj.getClass().getSimpleName(), obj.getClass());
		Object getObj=xstream.fromXML(xmlContent);
		return getObj;
	}
}



那么就输出了

<SexyWoman>
  <name>maggie</name>
  <high>172</high>
  <weight>65</weight>
  <car>
    <cardName>ppo</cardName>
  </car>
</SexyWoman>




maggie

你可能感兴趣的:(xstream)