xstream javabeanとxmlを互いに変換

9271 ワード

今日は、データベースのデータをlにエクスポートし、外部からインポートしてデータベースに保存する必要があります.
エクスポートされたデータフォーマットはxmlまたはjsonであることを考慮します.jsonならgoogleのgsonで実現できます.
以前やったことがあります.xmlにエクスポートすると、以前はjavaで綴ったりjdomやdom 4 jを使っていました.今日
xstreamも強力で、javaオブジェクトをxmlに変換したり、xmlからjavaに変換したりすることができます.
オブジェクト.専門的には、xmlにシーケンス化したりjavaオブジェクトにシーケンス化したりすることができます.もちろんxmlも完璧にサポートされています
jsonのシーケンス化と逆シーケンス化は,2つのモデル駆動を提供する.この2つのドライバでJavaオブジェクトからJSONへの
互いに転換する.JettisonMappedXmlDriverドライバを使用してJavaオブジェクトをjsonに変換するにはjettison.jarを追加する必要があります
以下は自分で書いたシミュレーション例です.JArとコードは添付ファイルにあります.
必要なjarはxstream-1.3.1.jar(必須)、xpp 3_min-1.1.4 c.jar(オプション)
 
 
Test.javaはjavaオブジェクトをxmlに変換
 
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;


public class Test
{
	/*
	 * java object to xml
	 */
	
	private static XmlBean xmlBean;
	public static void main(String[] args)
	{
		//instantiate the XStream class
		XStream xstream = new XStream();
		xstream.alias("step", Step.class);
		xstream.alias("action", Action.class);
		xstream.alias("flow", Flow.class);
		
		//Serializing an object to XML
		
		setData();
		
		String xml = xstream.toXML(xmlBean);
		System.out.println(xml);
	}
	//     
	public static void setData()
	{
		List<Step> stepList = new ArrayList<Step>();
		Step s;
		for(int i=0;i<5;i++)
		{
			s=new Step();
			s.setId(new Long(i));
			s.setSeq(new Long(i+i));
			s.setName("step"+i);
			stepList.add(s);
			
		}
		
		Action a;
		List<Action> actionList = new ArrayList<Action>();
		for(int i=0;i<5;i++)
		{
			a=new Action();
			a.setId(new Long(i));
			a.setIsSub(i+i);
			a.setName("action"+i);
			actionList.add(a);
			
		}
		
		Flow flow = new Flow();
		flow.setActionId(1L);
		flow.setClassId(3L);
		flow.setSclassId(3L);
		flow.setName("flow_analy");
		flow.setStepId(4L);
		flow.setActionId(5L);
		
		xmlBean = new XmlBean();
		xmlBean.setFlow(flow);
		xmlBean.setStepList(stepList);
		xmlBean.setActionList(actionList);
	}
}

 Test 1.javaはjavaオブジェクトをxmlに変換してファイルに書き込む
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;


public class Test2
{
	/*
	 * java object to xml
	 */
	
	private static XmlBean xmlBean;
	public static void main(String[] args) throws Exception
	{
		//instantiate the XStream class
		XStream xstream = new XStream();
		xstream.alias("step", Step.class);
		xstream.alias("action", Action.class);
		xstream.alias("flow", Flow.class);
		
		//Serializing an object to XML
		
		setData();
		
	
		OutputStream out = new FileOutputStream("c:/test.xml");
	    xstream.toXML(xmlBean, out);
	    out.close();
	}
	//     
	public static void setData()
	{
		List<Step> stepList = new ArrayList<Step>();
		Step s;
		for(int i=0;i<5;i++)
		{
			s=new Step();
			s.setId(new Long(i));
			s.setSeq(new Long(i+i));
			s.setName("  "+i);
			stepList.add(s);
			
		}
		
		Action a;
		List<Action> actionList = new ArrayList<Action>();
		for(int i=0;i<5;i++)
		{
			a=new Action();
			a.setId(new Long(i));
			a.setIsSub(i+i);
			a.setName("action"+i);
			actionList.add(a);
			
		}
		
		Flow flow = new Flow();
		flow.setActionId(1L);
		flow.setClassId(3L);
		flow.setSclassId(3L);
		flow.setName("flow_analy");
		flow.setStepId(4L);
		flow.setActionId(5L);
		
		xmlBean = new XmlBean();
		xmlBean.setFlow(flow);
		xmlBean.setStepList(stepList);
		xmlBean.setActionList(actionList);
	}
}

 Test3.java  ハードディスク(HDD)のファイルを読み込んでjavaオブジェクトに変換します.
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class Test3
{
	/**
	 * java object to xml
	 */
	
	private static XmlBean xmlBean;
	public static void main(String[] args) throws Exception
	{
		//instantiate the XStream class
		XStream xstream = new XStream();
		xstream.alias("step", Step.class);
		xstream.alias("action", Action.class);
		xstream.alias("flow", Flow.class);
		
		//Serializing an object to XML
		
		
		String xml =  readFile("c:/test.xml");
		System.out.println(xml);
		//         Specify another driver. For example: new XStream(new DomDriver())
		XmlBean bean = (XmlBean)xstream.fromXML(xml);
		System.out.println(bean);
		Flow flow = bean.getFlow();
		List<Step> steps = bean.getStepList();
		for(Step step : steps )
		{
			
		}
		List<Action> actions = bean.getActionList();
		
		
	}
	/**
	 *       
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	 public static String readFile(String fileName) throws Exception {
	    String fileContent = "";
		File f = new File(fileName);
		FileReader fileReader = new FileReader(f);
		BufferedReader reader = new BufferedReader(fileReader);
		String line = "";
		while ((line = reader.readLine()) != null)
		{
			 fileContent = fileContent + line;
		}
		reader.close();
		return fileContent;
	}
}

 
 
次の3つのjavabean、XStreamはあなたの属性の可視性を制限せず、デフォルトのすべての属性が変換されます.XStreamはsetterメソッドとgetterメソッドを必要としないし、デフォルトのクラス構築メソッドを必要としない.
Step.java
 
public class Step
{
	private Long id;
	private String name;
	private Long seq;
	private String remarks;
	public Long getId()
	{
		return id;
	}
	public void setId(Long id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Long getSeq()
	{
		return seq;
	}
	public void setSeq(Long seq)
	{
		this.seq = seq;
	}
	public String getRemarks()
	{
		return remarks;
	}
	public void setRemarks(String remarks)
	{
		this.remarks = remarks;
	}
	
	

}

 Action.java
 
public class Action
{
	private Long id;
	private String name;
	private int isSub;
	private String remarks;
	public Long getId()
	{
		return id;
	}
	public void setId(Long id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getIsSub()
	{
		return isSub;
	}
	public void setIsSub(int isSub)
	{
		this.isSub = isSub;
	}
	public String getRemarks()
	{
		return remarks;
	}
	public void setRemarks(String remarks)
	{
		this.remarks = remarks;
	}
	
	
}

 Flow.java
 
public class Flow
{
	private Long id;
	private String name;
	private Long classId;
	private Long sclassId;
	private Long stepId;
	private Long actionId;
	public Long getId()
	{
		return id;
	}
	public void setId(Long id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Long getClassId()
	{
		return classId;
	}
	public void setClassId(Long classId)
	{
		this.classId = classId;
	}
	public Long getSclassId()
	{
		return sclassId;
	}
	public void setSclassId(Long sclassId)
	{
		this.sclassId = sclassId;
	}
	public Long getStepId()
	{
		return stepId;
	}
	public void setStepId(Long stepId)
	{
		this.stepId = stepId;
	}
	public Long getActionId()
	{
		return actionId;
	}
	public void setActionId(Long actionId)
	{
		this.actionId = actionId;
	}
	
	
	
}