JAva実現XMLとObjectの相互回転---xstream

11979 ワード

XMLとObjectの相互回転を実現するツールはたくさんあります.ここではxstreamツールを使用しています.xpp 3.jarを一緒に使用する必要があります.本稿では、
xstream.1.4.jarおよび
xpp3-1.1.2.jar
本題に入る
Object転送XML:
Webプロジェクトの作成
新しいbeanクラス:CheckOut.java

package xmlParse;

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;

public class CheckOut {
	private String RoomNo;
	private String RoomTypeName;
	private String RoomBuildingName;
	private String RoomBuildingCode;
	private String RoomFloorName;
	private String RoomFloorCode;
	private String CheckInTime;
	private String CheckOutTime;
	private boolean CheckInRoom;
	private boolean CheckOutRoom;
	private List<GuestInfo> Guests;
	public String getRoomNo() {
		return RoomNo;
	}
	public void setRoomNo(String roomNo) {
		RoomNo = roomNo;
	}
	public String getRoomTypeName() {
		return RoomTypeName;
	}
	public void setRoomTypeName(String roomTypeName) {
		RoomTypeName = roomTypeName;
	}
	public String getRoomBuildingName() {
		return RoomBuildingName;
	}
	public void setRoomBuildingName(String roomBuildingName) {
		RoomBuildingName = roomBuildingName;
	}
	public String getRoomBuildingCode() {
		return RoomBuildingCode;
	}
	public void setRoomBuildingCode(String roomBuildingCode) {
		RoomBuildingCode = roomBuildingCode;
	}
	public String getRoomFloorName() {
		return RoomFloorName;
	}
	public void setRoomFloorName(String roomFloorName) {
		RoomFloorName = roomFloorName;
	}
	public String getRoomFloorCode() {
		return RoomFloorCode;
	}
	public void setRoomFloorCode(String roomFloorCode) {
		RoomFloorCode = roomFloorCode;
	}
	public String getCheckInTime() {
		return CheckInTime;
	}
	public void setCheckInTime(String checkInTime) {
		CheckInTime = checkInTime;
	}
	public String getCheckOutTime() {
		return CheckOutTime;
	}
	public void setCheckOutTime(String checkOutTime) {
		CheckOutTime = checkOutTime;
	}
	public boolean getCheckInRoom() {
		return CheckInRoom;
	}
	public void setCheckInRoom(boolean checkInRoom) {
		CheckInRoom = checkInRoom;
	}
	public List<GuestInfo> getGuests() {
		return Guests;
	}
	public void setGuests(List<GuestInfo> guests) {
		this.Guests = guests;
	}
	public boolean isCheckOutRoom() {
		return CheckOutRoom;
	}
	public void setCheckOutRoom(boolean checkOutRoom) {
		CheckOutRoom = checkOutRoom;
	}
	
	public CheckOut() {
		// TODO Auto-generated constructor stub
	}
	public CheckOut(String roomNo, String roomTypeName,
			String roomBuildingName, String roomBuildingCode,
			String roomFloorName, String roomFloorCode, String checkInTime,
			String checkOutTime, boolean checkInRoom, boolean checkOutRoom) {
		super();
		RoomNo = roomNo;
		RoomTypeName = roomTypeName;
		RoomBuildingName = roomBuildingName;
		RoomBuildingCode = roomBuildingCode;
		RoomFloorName = roomFloorName;
		RoomFloorCode = roomFloorCode;
		CheckInTime = checkInTime;
		CheckOutTime = checkOutTime;
		CheckInRoom = checkInRoom;
		CheckOutRoom = checkOutRoom;
	}
	
	
}


beanクラスGuestInfo.javaの作成

package xmlParse;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("GuestInfo")
public class GuestInfo {
	public String GuestName;
	public String IDCardNo;
	public String IDCardTypeId;
	public String Nationality;
	public String CountryCode;
	public String Gender;
	public String Birthday;
	public String Address;
	public String CityCode;
	public String ProvinceCode;
	public String VisaNo;
	public String VisaTypeId;
	public String VisaValid;
	
	public String getGuestName() {
		return GuestName;
	}
	public void setGuestName(String guestName) {
		GuestName = guestName;
	}
	public String getIDCardNo() {
		return IDCardNo;
	}
	public void setIDCardNo(String iDCardNo) {
		IDCardNo = iDCardNo;
	}
	public String getIDCardTypeId() {
		return IDCardTypeId;
	}
	public void setIDCardTypeId(String iDCardTypeId) {
		IDCardTypeId = iDCardTypeId;
	}
	public String getNationality() {
		return Nationality;
	}
	public void setNationality(String nationality) {
		Nationality = nationality;
	}
	public String getGender() {
		return Gender;
	}
	public void setGender(String gender) {
		Gender = gender;
	}
	public String getBirthday() {
		return Birthday;
	}
	public void setBirthday(String birthday) {
		Birthday = birthday;
	}
	public String getAddress() {
		return Address;
	}
	public void setAddress(String address) {
		Address = address;
	}
	public String getCityCode() {
		return CityCode;
	}
	public void setCityCode(String cityCode) {
		CityCode = cityCode;
	}
	public String getProvinceCode() {
		return ProvinceCode;
	}
	public void setProvinceCode(String provinceCode) {
		ProvinceCode = provinceCode;
	}
	public String getCountryCode() {
		return CountryCode;
	}
	public void setCountryCode(String countryCode) {
		CountryCode = countryCode;
	}
	public String getVisaNo() {
		return VisaNo;
	}
	public void setVisaNo(String visaNo) {
		VisaNo = visaNo;
	}
	public String getVisaTypeId() {
		return VisaTypeId;
	}
	public void setVisaTypeId(String visaTypeId) {
		VisaTypeId = visaTypeId;
	}
	public String getVisaValid() {
		return VisaValid;
	}
	public void setVisaValid(String visaValid) {
		VisaValid = visaValid;
	}
	public GuestInfo(String guestName, String iDCardNo, String iDCardTypeId,
			String nationality, String countryCode, String gender,
			String birthday) {
		super();
		GuestName = guestName;
		IDCardNo = iDCardNo;
		IDCardTypeId = iDCardTypeId;
		Nationality = nationality;
		CountryCode = countryCode;
		Gender = gender;
		Birthday = birthday;
	}

	
}

テストクラスXStreamTest.javaを作成します

import java.util.ArrayList;
import java.util.List;

import xmlParse.CheckOut;
import xmlParse.GuestInfo;

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


public class XStreamTest {

	public static void main(String[] args) {
		XStream xStream = new XStream(new DomDriver());
		CheckOut check = new CheckOut("1","12","13","14","15","16","17","18",true,false);
		List<GuestInfo> list = new ArrayList<GuestInfo>();
		for(int i =0; i < 2; i++){
			GuestInfo g = new GuestInfo("1", "2", "3", "4", "countryCode", "gender", "birthday");
			list.add(g);
		}
		check.setGuests(list);
		
		System.out.println(xStream.toXML(check));
	}

}


実行結果:
参照

  1
  12
  13
  14
  15
  16
  17
  18
  true
  false
 
   
      1
      2
      3
      4
      countryCode
      gender
      birthday
   

   
      1
      2
      3
      4
      countryCode
      gender
      birthday
   

 


ここではGuestInfoクラスとCheckOutクラスがフルパス名であることを解析します.クラス名のみを保持したい場合は、xstreamは2つの解決策を提供します.
1、alias(name,class)方法
xStream.alias("CheckOut", CheckOut.class);
xStream.alias("GuestInfo", GuestInfo.class);
2、注釈
クラス名に@XStreamAlias("your alias name")を付ける

package xmlParse;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("CheckOut")
public class CheckOut {}

package xmlParse;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("GuestInfo")
public class GuestInfo {}

xstreamインスタンス化オブジェクトでxStream.autodetectAnnotations(true);できます.
XML転送Object
ここでうまくいかないと変換エラーになるので、まずオブジェクトをxmlに変換し、そのフォーマットを見てからxmlをobjectに変換することをお勧めします.
CheckOut.javaを作成しました.
GuestInfo.javaを作成しました.
XSreamTest.javaテストクラスの作成

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import xmlParse.CheckOut;
import xmlParse.GuestInfo;

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


public class XStreamTest {

	public static void main(String[] args) throws FileNotFoundException {
		XStream xStream = new XStream(new DomDriver());
		xStream.alias("CheckOut", CheckOut.class);
		xStream.alias("GuestInfo", GuestInfo.class);
		InputStream input = new FileInputStream(new File("C:/Users/lucky god/Desktop/CheckOut.xml"));
		CheckOut obj = (CheckOut)xStream.fromXML(input);
		System.out.println(obj.toString());
	}

}

CheckOut.xmlの内容を添付します

<?xml version="1.0" encoding="UTF-8"?>
<CheckOut>
	<RoomNo/>
	<RoomTypeName/>
	<RoomBuildingName/>
	<RoomBuildingCode/>
	<RoomFloorName/>
	<RoomFloorCode/>
	<CheckInTime>2011-11-11 11:22:33</CheckInTime>
	<CheckOutTime>2011-11-11 11:22:33</CheckOutTime>
	<CheckOutRoom>true</CheckOutRoom>
	<Guests>
		<GuestInfo>
			<GuestName>1</GuestName>
			<IDCardNo>1</IDCardNo>
			<IDCardTypeId/>
			<Nationality/>
			<CountryCode/>
		</GuestInfo>
		<GuestInfo>
			<GuestName>2</GuestName>
			<IDCardNo>2</IDCardNo>
			<IDCardTypeId/>
			<Nationality/>
			<CountryCode/>
		</GuestInfo>
	</Guests>
</CheckOut>