XStream

时间:2021-05-09 18:52:14


1.引入需要的jar包,在pom.xml中配置依赖

  1. <dependency>
  2. <groupId>com.thoughtworks.xstream</groupId>
  3. <artifactId>xstream</artifactId>
  4. <version></version>
  5. </dependency>
<dependency>
	<groupId>com.thoughtworks.xstream</groupId>
	<artifactId>xstream</artifactId>
	<version>1.4.2</version>
</dependency>

2.创建要转换的实例类,本文以简单的硬件信息为例

  1. package com.ajita.xstream;
  2. import com.thoughtworks.xstream.annotations.XStreamAlias;
  3. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
  4. @XStreamAlias("DiskInfo")
  5. public class DiskInfo {
  6. @XStreamAsAttribute
  7. @XStreamAlias("diskName")
  8. private String name;
  9. private int totalSize;
  10. private int avilableSize;
  11. private int usedSize;
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public int getTotleSize() {
  19. return totalSize;
  20. }
  21. public void setTotleSize(int totleSize) {
  22. this.totalSize = totleSize;
  23. }
  24. public int getAvilableSize() {
  25. return avilableSize;
  26. }
  27. public void setAvilableSize(int avilableSize) {
  28. this.avilableSize = avilableSize;
  29. }
  30. public int getUserSize() {
  31. return usedSize;
  32. }
  33. public void setUserSize(int userSize) {
  34. this.usedSize = userSize;
  35. }
  36. public String toString() {
  37. StringBuilder sb = new StringBuilder();
  38. sb.append(name + "\r\n");
  39. sb.append("TotalSize:" + totalSize + "|Used Size:" + usedSize
  40. + "|Available Size:" + avilableSize);
  41. return sb.toString();
  42. }
  43. }
  44. package com.ajita.xstream;
  45. import java.util.List;
  46. import com.thoughtworks.xstream.annotations.XStreamAlias;
  47. @XStreamAlias("HardwareInfo")
  48. public class HardwareInfo {
  49. private String cpuInfo;
  50. private String networkInfo;
  51. private List<DiskInfo> diskInfos;
  52. public String getCpuInfo() {
  53. return cpuInfo;
  54. }
  55. public void setCpuInfo(String cpuInfo) {
  56. this.cpuInfo = cpuInfo;
  57. }
  58. public String getNetworkInfo() {
  59. return networkInfo;
  60. }
  61. public void setNetworkInfo(String networkInfo) {
  62. this.networkInfo = networkInfo;
  63. }
  64. public List<DiskInfo> getDiskInfos() {
  65. return diskInfos;
  66. }
  67. public void setDiskInfos(List<DiskInfo> diskInfos) {
  68. this.diskInfos = diskInfos;
  69. }
  70. public String toString() {
  71. StringBuilder sb = new StringBuilder();
  72. sb.append(cpuInfo);
  73. sb.append("\r\n");
  74. sb.append(networkInfo);
  75. sb.append("\r\n");
  76. for (DiskInfo disk : diskInfos) {
  77. sb.append(disk.toString());
  78. sb.append("\r\n");
  79. }
  80. return sb.toString();
  81. }
  82. }
package com.ajita.xstream;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("DiskInfo")
public class DiskInfo {
	@XStreamAsAttribute
	@XStreamAlias("diskName")
	private String name;
	private int totalSize;
	private int avilableSize;
	private int usedSize;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getTotleSize() {
		return totalSize;
	}

	public void setTotleSize(int totleSize) {
		this.totalSize = totleSize;
	}

	public int getAvilableSize() {
		return avilableSize;
	}

	public void setAvilableSize(int avilableSize) {
		this.avilableSize = avilableSize;
	}

	public int getUserSize() {
		return usedSize;
	}

	public void setUserSize(int userSize) {
		this.usedSize = userSize;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(name + "\r\n");
		sb.append("TotalSize:" + totalSize + "|Used Size:" + usedSize
				+ "|Available Size:" + avilableSize);
		return sb.toString();
	}
}

package com.ajita.xstream;

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("HardwareInfo")
public class HardwareInfo {
	private String cpuInfo;
	private String networkInfo;
	private List<DiskInfo> diskInfos;

	public String getCpuInfo() {
		return cpuInfo;
	}

	public void setCpuInfo(String cpuInfo) {
		this.cpuInfo = cpuInfo;
	}

	public String getNetworkInfo() {
		return networkInfo;
	}

	public void setNetworkInfo(String networkInfo) {
		this.networkInfo = networkInfo;
	}

	public List<DiskInfo> getDiskInfos() {
		return diskInfos;
	}

	public void setDiskInfos(List<DiskInfo> diskInfos) {
		this.diskInfos = diskInfos;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(cpuInfo);
		sb.append("\r\n");
		sb.append(networkInfo);
		sb.append("\r\n");
		for (DiskInfo disk : diskInfos) {
			sb.append(disk.toString());
			sb.append("\r\n");
		}
		return sb.toString();
	}
}

3.新建测试代码

  1. package com.ajita.xstream;
  2. import java.util.ArrayList;
  3. import com.thoughtworks.xstream.XStream;
  4. public class TestXStream {
  5. /**
  6. * @param args
  7. */
  8. public static void main(String[] args) {
  9. DiskInfo disk1 = new DiskInfo();
  10. disk1.setName("D:\\");
  11. disk1.setTotleSize();
  12. disk1.setUserSize();
  13. disk1.setAvilableSize();
  14. DiskInfo disk2 = new DiskInfo();
  15. disk2.setName("E:\\");
  16. disk2.setTotleSize();
  17. disk2.setUserSize();
  18. disk2.setAvilableSize();
  19. ArrayList<DiskInfo> diskInfos = new ArrayList<DiskInfo>();
  20. diskInfos.add(disk1);
  21. diskInfos.add(disk2);
  22. HardwareInfo hwInfo = new HardwareInfo();
  23. hwInfo.setCpuInfo("cpu info is ...");
  24. hwInfo.setNetworkInfo("network info is ...");
  25. hwInfo.setDiskInfos(diskInfos);
  26. XStream xStream = new XStream();
  27. xStream.autodetectAnnotations(true);
  28. String xml = xStream.toXML(hwInfo);
  29. System.out.println(xml);
  30. HardwareInfo info2 = (HardwareInfo) xStream.fromXML(xml);
  31. System.out.println(info2);
  32. }
  33. }
package com.ajita.xstream;

import java.util.ArrayList;

import com.thoughtworks.xstream.XStream;

public class TestXStream {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DiskInfo disk1 = new DiskInfo();
		disk1.setName("D:\\");
		disk1.setTotleSize(100);
		disk1.setUserSize(20);
		disk1.setAvilableSize(80);
		DiskInfo disk2 = new DiskInfo();
		disk2.setName("E:\\");
		disk2.setTotleSize(200);
		disk2.setUserSize(40);
		disk2.setAvilableSize(160);

		ArrayList<DiskInfo> diskInfos = new ArrayList<DiskInfo>();
		diskInfos.add(disk1);
		diskInfos.add(disk2);

		HardwareInfo hwInfo = new HardwareInfo();
		hwInfo.setCpuInfo("cpu info is ...");
		hwInfo.setNetworkInfo("network info is ...");
		hwInfo.setDiskInfos(diskInfos);

		XStream xStream = new XStream();
		xStream.autodetectAnnotations(true);
		String xml = xStream.toXML(hwInfo);
		System.out.println(xml);

		HardwareInfo info2 = (HardwareInfo) xStream.fromXML(xml);
		System.out.println(info2);
	}

}

输出如下:

  1. <HardwareInfo>
  2. <cpuInfo>cpu info is ...</cpuInfo>
  3. <networkInfo>network info is ...</networkInfo>
  4. <diskInfos>
  5. <DiskInfo diskName="D:\">
  6. <totalSize></totalSize>
  7. <avilableSize></avilableSize>
  8. <usedSize></usedSize>
  9. </DiskInfo>
  10. <DiskInfo diskName="E:\">
  11. <totalSize></totalSize>
  12. <avilableSize></avilableSize>
  13. <usedSize></usedSize>
  14. </DiskInfo>
  15. </diskInfos>
  16. </HardwareInfo>
  17. cpu info is ...
  18. network info is ...
  19. D:\
  20. TotalSize:|Used Size:|Available Size:
  21. E:\
  22. TotalSize:|Used Size:|Available Size:
<HardwareInfo>
  <cpuInfo>cpu info is ...</cpuInfo>
  <networkInfo>network info is ...</networkInfo>
  <diskInfos>
    <DiskInfo diskName="D:\">
      <totalSize>100</totalSize>
      <avilableSize>80</avilableSize>
      <usedSize>20</usedSize>
    </DiskInfo>
    <DiskInfo diskName="E:\">
      <totalSize>200</totalSize>
      <avilableSize>160</avilableSize>
      <usedSize>40</usedSize>
    </DiskInfo>
  </diskInfos>
</HardwareInfo>
cpu info is ...
network info is ...
D:\
TotalSize:100|Used Size:20|Available Size:80
E:\
TotalSize:200|Used Size:40|Available Size:160

4.常用XStream注解

1)@XStreamAlias注解可在类与属性上使用设置名称,相当于: xstream.alias("blog", Blog.class);

2)@XStreamImplicit去集合节点名:相当于 xstream.addImplicitCollection(Blog.class, "entries");


3) @XStreamConverter(SingleValueCalendarConverter.class)

    private Calendar createdTime = new GregorianCalendar();