欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实现XML与Object互转---xstream

程序员文章站 2022-06-15 19:21:38
...
实现XML与Object互转的工具有很多,这里使用的是xstream工具,需要xpp3.jar一起使用才可正常运行,本文用到的是xstream.1.4.jarxpp3-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));
	}

}


运行的结果为:
引用

<xmlParse.CheckOut>
  <RoomNo>1</RoomNo>
  <RoomTypeName>12</RoomTypeName>
  <RoomBuildingName>13</RoomBuildingName>
  <RoomBuildingCode>14</RoomBuildingCode>
  <RoomFloorName>15</RoomFloorName>
  <RoomFloorCode>16</RoomFloorCode>
  <CheckInTime>17</CheckInTime>
  <CheckOutTime>18</CheckOutTime>
  <CheckInRoom>true</CheckInRoom>
  <CheckOutRoom>false</CheckOutRoom>
  <Guests>
    <xmlParse.GuestInfo>
      <GuestName>1</GuestName>
      <IDCardNo>2</IDCardNo>
      <IDCardTypeId>3</IDCardTypeId>
      <Nationality>4</Nationality>
      <CountryCode>countryCode</CountryCode>
      <Gender>gender</Gender>
      <Birthday>birthday</Birthday>
    </xmlParse.GuestInfo>
    <xmlParse.GuestInfo>
      <GuestName>1</GuestName>
      <IDCardNo>2</IDCardNo>
      <IDCardTypeId>3</IDCardTypeId>
      <Nationality>4</Nationality>
      <CountryCode>countryCode</CountryCode>
      <Gender>gender</Gender>
      <Birthday>birthday</Birthday>
    </xmlParse.GuestInfo>
  </Guests>
</xmlParse.CheckOut>

这里解析出来GuestInfo和CheckOut类都是全路径名 , 如果你只想只保留类名,xstream提供了两种解决方式:
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>

相关标签: java xml xstream