xStream 的简单使用 xml to bean

前端之家收集整理的这篇文章主要介绍了xStream 的简单使用 xml to bean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在写这篇技术blog时,我参看了 http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html

也可以看看这篇,写的挺细的,我写了一个xml 到 bean. 和bean 到xml 转换的一个例子,

一来,作为成长的一个脚印,二来,总结一些代码,以后备用,在银行和保险方面,在接口的传送数据时,经常传送的是xml报文。

有的加密,有你的明文,都同样需要这样一个转换的功能。所以来写这篇blog,主要是自己看,如果,别的攻城师 看到这篇blog,

并学会了一些处理业务的方法,那是我的荣幸,我会非常的高兴。

一、准备工作

1、 下载jar包、及官方资源

xStream的jar下载地址:

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

官方的示例很全,官方参考示例:http://xstream.codehaus.org/tutorial.html

2、项目的整体截图:

3.1、分析:自己要创建一个xml 文件,格式:一个News下有多个school,一个school下有多个teacher,一个teacher下有多个student. 如图:

3.2、针对上述的xml报文解析成我们需要的对象。

4.编写拼写xml 报文的结构。

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 age) {
		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;
	}
    
}


注:按上边所需要的xml 报文结构编写java 代码

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<Student>();
	   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("姓名",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();
	   	
   }
   /**
	 * 获取xml报文
	 * @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;
	} 
}

注:在
xstream.alias("com.test.picc.domain.News",News.class); 是为了方便xStream 的转换之用,如果一个类中只有一个News.class 就不用此项气操作。

结果:

demo 下载地址:http://download.csdn.net/detail/liuc0317/5389673

猜你在找的XML相关文章