XStream 用法详解 XML 转换为 java 实体类

前端之家收集整理的这篇文章主要介绍了XStream 用法详解 XML 转换为 java 实体类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

XStream 用法详解 java 类与 XML 互换

现在 WEB数据交换的时代,传送XML目前是一个比较流行的方式,具有统一的规则约束,为实现后台接口提供了一个很方便的实现。

我编写了一个 接收XML并转换成所需要的Object类的 小例子,希望能够对做互联网数据传输、接口调用的朋友有所帮助。

首先要导入jar包xstream-1.4.3-sources.jar 和 xmlpull-1.1.3.1.jar两个包;

其次是预备一个 XML 事例

<config>
<span style="white-space:pre">	</span><client type="8888" osversion="9999" version="123" oemtag="5555" area="areacode"/>
<span style="white-space:pre">	</span><protocol>1.10</protocol>
<span style="white-space:pre">	</span><sign value="asdfasdf"/>
<span style="white-space:pre">	</span><vientiance version="version"/>
</config>

其次 就是 按照 XML节点的顺序 从外 到内 编写 java PO类,此实例的目的是将上面的XML转换为AllnewstateRQ 这个实体类,然后从里面打印出测试数据。

下面依次是 config节点对应的AllnewstateRQ类; client 节点 对应的 Client 类;sign 节点对应的 Sign类;vientiance 节点 对应的 Vientiance类。

package com.wgq.test09_xml;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("config")
public class AllnewstateRQ {
	//当节点下有独立属性的时候,需要创建一个独立的类用来保存节点内的属性
	private Client client = new Client();
	private Sign sign = new Sign();
	private Vientiance vientiance = new Vientiance();
	//当节点下没有属性,直接由StringValue的时候可直接创建String类型属性
	private String protocol;
	public Client getClient() {
		return client;
	}
	public void setClient(Client client) {
		this.client = client;
	}
	public Sign getSign() {
		return sign;
	}
	public void setSign(Sign sign) {
		this.sign = sign;
	}
	public Vientiance getVientiance() {
		return vientiance;
	}
	public void setVientiance(Vientiance vientiance) {
		this.vientiance = vientiance;
	}
	public String getProtocol() {
		return protocol;
	}
	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}
}

package com.wgq.test09_xml;

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

@XStreamAlias("client")
public class Client {

	@XStreamAsAttribute	//对属性值进行注解
	private String type;//此时类中的属性名要和xml内的属性名相对应

	@XStreamAsAttribute
	private String osversion;

	@XStreamAsAttribute
	private String version;

	@XStreamAsAttribute
	private String oemtag;

	@XStreamAsAttribute
	private String area;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getOsversion() {
		return osversion;
	}

	public void setOsversion(String osversion) {
		this.osversion = osversion;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getOemtag() {
		return oemtag;
	}

	public void setOemtag(String oemtag) {
		this.oemtag = oemtag;
	}

	public String getArea() {
		return area;
	}

	public void setArea(String area) {
		this.area = area;
	}

}

package com.wgq.test09_xml;

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

@XStreamAlias("sign") //此处要对应节点的名称
public class Sign {
	@XStreamAsAttribute
	private String value;//此处对应节点内属性名称
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
}

package com.wgq.test09_xml;

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

@XStreamAlias("vientiance")
public class Vientiance {
	@XStreamAsAttribute
	private String version;
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
}

测试main方法
package com.wgq.test09_xml;

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

public class TestStream {

	public static void main(String[] args) {
		String reqXml = getXml();
		XStream xs = new XStream(new DomDriver()); 
		xs.processAnnotations(new Class[]{AllnewstateRQ.class,Client.class,Sign.class,Vientiance.class});
		Object obj = xs.fromXML(reqXml);
		AllnewstateRQ allnewstateRQ = (AllnewstateRQ) obj;
		System.out.println(allnewstateRQ.getProtocol());
		System.out.println(allnewstateRQ.getClient().getArea());
		System.out.println(reqXml);

	}
	
	static String getXml(){
		StringBuilder str = new StringBuilder();
		str.append("")
		.append("<config>")
		.append("<client type=\"8888\" osversion=\"9999\" version=\"123\" oemtag=\"5555\"  area=\"areacode\" />")
		.append("<protocol>1.10</protocol>")
		.append("<sign value=\"asdfasdf\" />")
		.append("<vientiance version=\"version\" />")
		.append("</config>")
		;
		return str.toString();
	}

}


输出结果:
1.10
areacode
<config><client type="8888" osversion="9999" version="123" oemtag="5555"  area="areacode" /><protocol>1.10</protocol><sign value="asdfasdf" /><vientiance version="version" /></config>



感谢 博主德莫罗的http://chen-rojer-gmail-com.iteye.com/blog/1435254范例,

我参考德莫罗的写的这个单元测试,将XML 转换成 指定的Object类的小例子.

猜你在找的XML相关文章