XStream simple use of XML to Bean

Source: Internet
Author: User

In writing this technical blog, I see the http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html

Can also look at this, write very thin, I wrote an XML to the bean. And a bean-to-XML conversion example,

One, as a growth of a footprint, and second, summed up some code, later, in the bank and insurance, in the interface of the transmission of data, is often transmitted by the XML message.

Some encryption, your plaintext, are also required such a conversion function. So to write this blog, the main is their own look, if, other siege division see this blog,

and learned some ways to deal with the business, it is my pleasure, I will be very happy.

First, the preparatory work

1, download the jar package, and official resources

XStream's jar download address:

Https://nexus.codehaus.org/content/repositories/releases/com/thoughtworks/xstream/xstream-distribution/1.3.1/ Xstream-distribution-1.3.1-bin.zip

The official example is full, official reference example: http://xstream.codehaus.org/tutorial.html

2, the overall screenshot of the project:

3.1, analysis: Their own to create an XML file, format: a news under a number of school, a school under a plurality of teacher, a teacher under a plurality of student. As shown in figure:

3.2. The XML messages above are parsed into the objects we need.

4. Write the structure of the spelling XML message.

News.java

Package com.test.picc.domain;

Import java.util.ArrayList;
Import java.util.List;

public class News {
   private list<school> schools = new arraylist<school> ();

	Public list<school> Getschools () {
		return schools;
	}
	
	public void Setschools (list<school> schools) {
		this.schools = schools;
	}
   
}

School.java

Package com.test.picc.domain;

Import java.util.ArrayList;
Import java.util.List;

public class School {
	private int id;
	private String name;
	Private String address;
	Private list<teacher> teachers= new arraylist<teacher> ();
	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;
	}
	Public String getaddress () {
		return address;
	}
	public void setaddress (String address) {
		this.address = address;
	}
	Public list<teacher> getteachers () {
		return teachers;
	}
	public void Setteachers (list<teacher> teachers) {
		this.teachers = teachers;
	}
	@Override public
	String toString () {
		 
		return  this.id + this.name;
	}
	

}


Teacher.java

Package com.test.picc.domain;

Import java.util.ArrayList;
Import java.util.List;

public class Teacher {
    private int id;
    private String name;
    private  int age;
    Private  list<student> students = new arraylist<student> ();
	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;
	}
	public int getage () {
		return age;
	}
	public void Setage (int.) {
		this.age = age;
	}
	 
	Public list<student> getstudents () {
		return students;
	}
	public void Setstudents (list<student> students) {
		this.students = students;
	}
	@Override public
	String toString () {
		 
		return this.id+ this.name;
	}
    
}

Student.java

Package com.test.picc.domain;

public class Student {
    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;
	}
    
}


Note: Write Java code according to the XML message structure required above.

Beantoxml.java

Package COM.TEST.PICC;
Import java.util.ArrayList;

Import java.util.List;
Import Com.test.picc.domain.News;
Import Com.test.picc.domain.School;
Import com.test.picc.domain.Student;
Import Com.test.picc.domain.Teacher;
Import Com.thoughtworks.xstream.XStream;

Import Com.thoughtworks.xstream.io.xml.DomDriver; public class Beantoxml {public static void main (string[] args) {list<student> stus = new Arraylist<stude
	   Nt> ();
	   list<student> Stus1 = new arraylist<student> ();
	   list<teacher> teas = new arraylist<teacher> ();
	   List<school> schools = new arraylist<school> ();
	   News New1 = new News ();
	   New1.setschools (schools);
	   School s1= new School ();
	   S1.setid (1);
	   S1.setname ("Beijing School");
	   S1.setaddress ("Beijin");
	   S1.setteachers (teas);
	   
	   Schools.add (S1);
	   Teacher T1 = new Teacher ();
	   T1.setid (1);
	   T1.setage (54);
	   T1.setname ("Zs");
	   
	   T1.setstudents (Stus); Teacher t2 = new TEACHer ();
	   T2.setid (2);
	   T2.setage (52);
	   T2.setname ("LS");
	   
	   T2.setstudents (STUS1);
	   Teas.add (t1);
	   
	   Teas.add (T2);
	   Student st1= new Student ();
	   St1.setid (1);
	   St1.setname ("Student1");
	   Student st2= new Student ();
	   St2.setid (1);
	   St2.setname ("Student2");
	   Stus.add (ST1);
	   
	   Stus.add (ST2);
	   Student st3= new Student ();
	   St3.setid (3);
	   St3.setname ("Student3");
	   Student st4= new Student ();
	   St4.setid (1);
	   St4.setname ("Student4");
	   Stus1.add (ST3);
Stus1.add (ST4);
	   System.out.println (S1.getteachers (). Get (0));
	   XStream XStream = new XStream (new Domdriver ());
       Xstream.aliaspackage ("", "Com.test.picc.domain");   
	   Xstream.aliasfield ("name", Student.class, "name");
String XML = Xstream.toxml (New1);
	   SYSTEM.OUT.PRINTLN (XML);
	   /** Xstream.alias ("Com.test.picc.domain.News", News.class);
	   Xstream.alias ("school", School.class);
	   Xstream.alias ("Teacher", Teacher.class); Xstream.alias ("StudeNT ", Student.class);
	   * * * * String news= getXml (XML, "News");
	   News N1 = (news) xstream.fromxml (news);
	   list<school> schools1 = N1.getschools ();
		   for (School sch:schools1) {System.out.println (Sch);
		   For (Teacher t:sch.getteachers ()) {System.out.println (t);
	   	
   }} System.out.println (); }/** * gets XML message * @param XML * @param node * @return */public static string getXml (string XML, String node
		{int startbodyvalue = Xml.indexof ("<" + node + ">");
		if (startbodyvalue<0) {return "";
		} if (XML! = null && xml.length () > 0) {xml = xml.substring (Startbodyvalue);
		} int indexbodyvalue = Xml.indexof ("</" + node + ">");
		if (indexbodyvalue<0) {return null;
		} if (XML! = null && xml.length () > 0) {xml = xml.substring (0, Indexbodyvalue);
		} XML = XML + "</" + node + ">";
	return XML;
 } 
}

Note: in
Xstream.alias ("Com.test.picc.domain.News", News.class); is to facilitate the conversion of xstream, if only one news.class in a class does not use this gas operation.

Results:

Demo Download Address: http://download.csdn.net/detail/liuc0317/5389673

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.