XStream usage Java class and XML interchange
now in the era of Web data exchange, the transfer of XML is currently a popular way, with uniform rules constraints, for the implementation of the background interface provides a very convenient implementation.
I wrote a small example of receiving XML and converting it into the object class I needed, in the hope of being able to help friends who do Internet data transfer and interface calls.
First, you import jar package Xstream-1.4.3-sources.jar and Xmlpull-1.1.3.1.jar two packages;
the second is to prepare an XML case
<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>
The second is to write the Java PO class from outside to within the order of the XML nodes, the purpose of which is to convert the above XML to the Allnewstaterq entity class, and then print out the test data from inside.
The following is the Allnewstaterq class corresponding to the config node in turn; Client node corresponding to the client class, sign node corresponding to the sign class, vientiance node corresponding Vientiance class.
Package com.wgq.test09_xml;
Import Com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias ("config") public
class Allnewstaterq {
//when there are independent properties under the node, you need to create a separate class to hold the attributes in the node
private Client client = new client ();
Private Sign Sign = new Sign ();
Private Vientiance vientiance = new Vientiance ();
When there is no attribute under the node, direct creation of String type attribute
private string protocol directly by StringValue;
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//annotation of attribute values private String type;//The property name in the class is relative to the property name within the XML
Should @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")//here the name of the corresponding node is public
class sign {
@XStreamAsAttribute
private String value;// The attribute name in the corresponding node here is 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;
}
}
Testing the Main method
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 ();
}
}
Output results:
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>
Thanks to the http://chen-rojer-gmail-com.iteye.com/blog/1435254 example of blogger Demoro,
I refer to this unit test written by Demoro to convert XML into a small example of the specified object class.