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 > </span><client type="8888" osversion="9999" version="123" oemtag="5555" area="areacode"/>
<span > </span><protocol>1.10</protocol>
<span > </span><sign value="asdfasdf"/>
<span > </span><vientiance version="version"/>
</config>
其次就是按照 XML节点的顺序从外到内编写 java PO类,此实例的⽬的是将上⾯的XML转换为 AllnewstateRQ 这个实体类,然后从⾥⾯打印出测试数据。
下⾯依次是 config节点对应的 AllnewstateRQ类 ; client 节点对应的 Client 类;sign 节点对应的 Sign类;vientiance 节点对应的Vientiance类。
package st09_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 st09_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) {
}
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) {
}
java xml是什么public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
}
package st09_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 st09_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 st09_xml;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.l.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.Protocol());
System.out.Client().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>")
;
String();
}
}
输出结果:
1.10
areacode
<config><client type="8888" osversion="9999" version="123" oemtag="5555" area="areacode" /><protocol>1.10</protocol><sign value="asdfasdf" /><vientiance 我参考德莫罗的写的这个单元测试,将XML 转换成指定的Object类的⼩例⼦.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论