1个回答
2016-07-20 · 百度知道合伙人官方认证企业
兄弟连教育
兄弟连教育成立于2006年,11年来专注IT职业教育,是国内专业的IT技术培训学校。2016年成功挂牌新三板(股票代码:839467)市值过亿。开设专注程序员培训专注php、Java、UI、云计算、Python、HTML5、
向TA提问
关注
展开全部
XStream 用法详解 java 类与 XML 互换
现在 WEB数据交换的时代,传送XML目前是一个比较流行的方式,具有统一的规则约束,为实现后台接口提供了一个很方便的实现。
我编写了一个 接收XML并转换成所需要的Object类的 小例子,希望能够对做互联网数据传输、接口调用的朋友有所帮助。
首先要导入jar包xstream-1.4.3-sources.jar 和 xmlpull-1.1.3.1.jar 两个包;
其次是预备一个 XML 事例
[html] view plain copy
<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类。
[java] view plain copy
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;
}
}
[java] view plain copy
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;
}
}
[java] view plain copy
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;
}
}
[java] view plain copy
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方法
[java] view plain copy
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();
}
}
输出结果:
[java] view plain copy
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>
现在 WEB数据交换的时代,传送XML目前是一个比较流行的方式,具有统一的规则约束,为实现后台接口提供了一个很方便的实现。
我编写了一个 接收XML并转换成所需要的Object类的 小例子,希望能够对做互联网数据传输、接口调用的朋友有所帮助。
首先要导入jar包xstream-1.4.3-sources.jar 和 xmlpull-1.1.3.1.jar 两个包;
其次是预备一个 XML 事例
[html] view plain copy
<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类。
[java] view plain copy
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;
}
}
[java] view plain copy
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;
}
}
[java] view plain copy
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;
}
}
[java] view plain copy
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方法
[java] view plain copy
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();
}
}
输出结果:
[java] view plain copy
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>
追问
这个和我问的好像没多大关系啊
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询