XStream Convert map and List

Source: Internet
Author: User

This article address: http://blog.csdn.net/kongxx/article/details/6946517

Nonsense, is not want to define too many Java beans, sometimes the need is simpler, think directly with Java Collection class to replace too many Java bean definitions, see the following code example

Package org.garbagecan.xstreamstudy.converter;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;

Import Java.util.Map;
Import Com.thoughtworks.xstream.XStream;

Import Com.thoughtworks.xstream.io.xml.DomDriver; public class Testmaplistconverter {@SuppressWarnings ({"Rawtypes", "Unchecked"}) public static void main (string[] args
		{XStream XStream = new XStream (new Domdriver ());
		
		Map map = new HashMap ();
		List List1 = new ArrayList ();
		List1.add (New T ("A1", "B1", "C1"));
		List1.add (New T ("A2", "B2", "C2"));
		List list2 = new ArrayList ();
		List2.add (New T ("A3", "B3", "C3"));
		List2.add (New T ("A4", "B4", "C4"));
		Map.put ("T1", List1);
		
		Map.put ("T2", list2);
		Xstream.alias ("Classes", Map.class);
		Xstream.alias ("Class", Map.Entry.class);
		
		Xstream.alias ("name", String.class);
		Xstream.alias ("Fields", list.class);
		Xstream.alias ("field", T.class);
		Xstream.aliasattribute (T.class, "a", "a"); Xstream.aliasattribute (T.class, "B", "B");
		Xstream.aliasattribute (T.class, "C", "C");

		SYSTEM.OUT.PRINTLN (Xstream.toxml (map));
	SYSTEM.OUT.PRINTLN (map) xstream.fromxml (xstream.toxml (map));
		Class T {public T (string A, string B, string c) {this.a = A;
		this.b = b;
	THIS.C = C;
	} private String A;
	Private String B;
	Private String C;
	Public String Geta () {return A;
	} public void SetA (String a) {this.a = A;
	Public String Getb () {return b;
	public void Setb (String b) {this.b = b;
	Public String getc () {return C;
	public void SetC (String c) {this.c = C; @Override public String toString () {return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString
	);
 }
}

Run the example above to get the XML inside as follows:

<classes>
  <class>
    <name>t2</name>
    <fields>
      <field a= "A3" b= "B3" C= "C3"/>
      <field a= "A4" b= "B4" c= "C4"/>
    </fields>
  </class>
  <class>
    <name>t1</name>
    <fields>
      <field a= "A1" b= "B1" c= "C1"/> <field a=
      "A2" b= "B2" "c=" C2 "/>
    </fields>
  </class>
</classes>
The contents of the resulting Java object are as follows:

{T2=[ORG.GARBAGECAN.XSTREAMSTUDY.CONVERTER.T@B166B5[A=A3,B=B3,C=C3], org.garbagecan.xstreamstudy.converter.t@ 1837697[A=A4,B=B4,C=C4]], T1=[ORG.GARBAGECAN.XSTREAMSTUDY.CONVERTER.T@1DECDEC[A=A1,B=B1,C=C1], ORG.GARBAGECAN.XSTREAMSTUDY.CONVERTER.T@A1807C[A=A2,B=B2,C=C2]]}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.