XStream在实现Java、xml之间转换非常的出色。但是有时候一些比较复杂的XML文件就不好下手,在此以如下XML为例演示了XML to Object,Object to XML的过程。
依赖包:
xstream-1.3.1.jar
xpp3_min-1.1.4c.jar
依赖包:
xstream-1.3.1.jar
xpp3_min-1.1.4c.jar
XML文件:
定义实体类:
- <IR>
- <UserDetails>
- <UID>guest</UID>
- <UType>JLJE89dd8DasGds6es</UType>
- <UToken>DG8kxkSEO903kdm8e2j4Kds8d</UToken>
- </UserDetails>
- <MainContext>
- <CID>ctx000001</CID>
- <Banner>
- <RepName>repname</RepName>
- </Banner>
- <GuidedFlows>
- <GFContext GFType="nav">
- <GFCID>gf0001</GFCID>
- <GFRep>
- <RepName>test</RepName>
- </GFRep>
- <Method Action="method">http://fdsf....</Method>
- <Parameters>
- <Param>
- <Location>location</Location>
- <ValidateMethod>validate method</ValidateMethod>
- </Param>
- </Parameters>
- </GFContext>
- <GFContext GFType="nav">
- <GFCID>gf0002</GFCID>
- <GFRep>
- <RepName>test2</RepName>
- </GFRep>
- <Method Action="method2">http://fdsf....</Method>
- <Parameters>
- <Param>
- <Location>location2</Location>
- <ValidateMethod>validate method2</ValidateMethod>
- </Param>
- </Parameters>
- </GFContext>
- <GFContext GFType="nav">
- <GFCID>gf0003</GFCID>
- <GFRep>
- <RepName>test3</RepName>
- </GFRep>
- <Method Action="method3">http://fdsf....</Method>
- <Parameters>
- <Param>
- <Location>location3</Location>
- <ValidateMethod>validate method3</ValidateMethod>
- </Param>
- </Parameters>
- </GFContext>
- </GuidedFlows>
- </MainContext>
- </IR>
UserDetails:
- public class UserDetails {
- private String UID;
- private String UType;
- private String UToken;
- public UserDetails(String UID,String UType,String uToken) {
- this.UID = UID;
- this.UType = UType;
- this.UToken = uToken;
- }
- public String getUID() {
- return UID;
- }
- public void setUID(String uID) {
- UID = uID;
- }
- public String getUType() {
- return UType;
- }
- public void setUType(String uType) {
- UType = uType;
- }
- public String getUToken() {
- return UToken;
- }
- public void setUToken(String uToken) {
- UToken = uToken;
- }
- @Override
- public String toString() {
- return "UserDetails [UID=" + UID + ",UType=" + UType + ",UToken="
- + UToken + "]";
- }
- }
MainContext:
- import java.util.ArrayList;
- import java.util.List;
- public class MainContext {
- private String CID;
- private Banner Banner;
- private List<GFContext> GFContext = new ArrayList<GFContext>();
- public MainContext(String CID,Banner Banner,List<GFContext> GFContext) {
- this.CID = CID;
- this.Banner = Banner;
- this.GFContext = GFContext;
- }
- public String getCID() {
- return CID;
- }
- public void setCID(String cID) {
- CID = cID;
- }
- public Banner getBanner() {
- return Banner;
- }
- public void setBanner(Banner banner) {
- Banner = banner;
- }
- public List<GFContext> getGFContext() {
- return GFContext;
- }
- public void setGFContext(List<GFContext> gFContext) {
- GFContext = gFContext;
- }
- @Override
- public String toString() {
- return "MainContext [CID=" + CID + ",Banner=" + Banner
- + ",GuidedFlows=" + GFContext + "]";
- }
- }
Banner:
- public class Banner {
- private String RepName;
- public Banner(String RepName){
- this.RepName = RepName;
- }
- public String getRepName() {
- return RepName;
- }
- public void setRepName(String repName) {
- RepName = repName;
- }
- @Override
- public String toString() {
- return "Banner [RepName=" + RepName + "]";
- }
- }
GFContext:
- public class GFContext {
- private String GFType;
- private String GFCID;
- private GFRep GFRep;
- private Method Method;
- private Parameters Parameters;
- public GFContext(String GFType,String GFCID,GFRep GFRep,Method Method,Parameters Parameters){
- this.GFType = GFType;
- this.GFCID = GFCID;
- this.GFRep = GFRep;
- this.Method = Method;
- this.Parameters = Parameters;
- }
- public String getGFType() {
- return GFType;
- }
- public void setGFType(String gFType) {
- this.GFType = gFType;
- }
- public String getGFCID() {
- return GFCID;
- }
- public void setGFCID(String gFCID) {
- this.GFCID = gFCID;
- }
- public GFRep getGFRep() {
- return GFRep;
- }
- public void setGFRep(GFRep gFRep) {
- this.GFRep = gFRep;
- }
- public Method getMethod() {
- return Method;
- }
- public void setMethod(Method Method) {
- this.Method = Method;
- }
- public Parameters getParameters() {
- return Parameters;
- }
- public void setParameters(Parameters parameters) {
- this.Parameters = parameters;
- }
- @Override
- public String toString() {
- return "GFContext [GFType=" + GFType + ",GFCID=" + GFCID + ",GFRep="
- + GFRep + ",Method=" + Method + ",Parameters=" + Parameters
- + "]";
- }
- }
GFRep:
- public class GFRep {
- private String RepName;
- private String RepURL;
- public GFRep(String RepName) {
- this.RepName = RepName;
- }
- public GFRep(String RepName,String RepURL) {
- this.RepName = RepName;
- this.RepURL = RepURL;
- }
- public String getRepName() {
- return RepName;
- }
- public void setRepName(String repName) {
- RepName = repName;
- }
- public String getRepURL() {
- return RepURL;
- }
- public void setRepURL(String repURL) {
- RepURL = repURL;
- }
- @Override
- public String toString() {
- return "GFRep [RepName=" + RepName + ",RepURL=" + RepURL + "]";
- }
- }
Method:
- public class Method {
- private String Action;
- private String value;
- public Method(){
- }
- public Method(String value){
- this.value = value;
- }
- public Method(String Action,String value){
- this.Action = Action;
- this.value = value;
- }
- public String getAction() {
- return Action;
- }
- public void setAction(String action) {
- Action = action;
- }
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- @Override
- public String toString() {
- return "Method [Action=" + Action + ",value=" + value + "]";
- }
- }
Parameters:
- public class Parameters {
- private Param Param;
- public Parameters(Param Param) {
- this.Param = Param;
- }
- public Param getParam() {
- return Param;
- }
- public void setParam(Param param) {
- Param = param;
- }
- @Override
- public String toString() {
- return "Parameters [Param=" + Param + "]";
- }
- }
Param:
- <span style="font-family:Courier New;font-size:12px;">public class Param {
- private String Location;
- private String ValidateMethod;
- public Param(String Location) {
- this.Location = Location;
- }
- public Param(String Location,String ValidateMethod) {
- this.Location = Location;
- this.ValidateMethod = ValidateMethod;
- }
- public String getLocation() {
- return Location;
- }
- public void setLocation(String location) {
- Location = location;
- }
- public String getValidateMethod() {
- return ValidateMethod;
- }
- public void setValidateMethod(String validateMethod) {
- ValidateMethod = validateMethod;
- }
- @Override
- public String toString() {
- return "Param [location=" + Location + ",validateMethod="
- + ValidateMethod + "]";
- }
- }</span>
由于我们需要实现类似:<field name="value">I am a Field.</Method>这样的效果。因此,我们需要实现一个Converter。这个很关键,也是我试了好久才解决的问题,在此也分享一下:
MethodConverter.java:
- <span style="font-family:Courier New;font-size:12px;">import com.thoughtworks.xstream.converters.Converter;
- import com.thoughtworks.xstream.converters.MarshallingContext;
- import com.thoughtworks.xstream.converters.UnmarshallingContext;
- import com.thoughtworks.xstream.io.HierarchicalStreamReader;
- import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
- public class MethodConverter implements Converter {
- public boolean canConvert(Class clazz) {
- return clazz.equals(Method.class);
- }
- public void marshal(Object value,HierarchicalStreamWriter writer,MarshallingContext context) {
- Method method = (Method)value;
- if (method!=null) {
- writer.addAttribute("Action",method.getAction());
- writer.setValue(method.getValue());
- }
- }
- @Override
- public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
- Method method = new Method();
- String action = reader.getAttribute("Action");
- method.setAction(action);
- System.out.println(">>> Action: "+action);
- String value = reader.getValue();
- System.out.println(">>> method: "+value+",NodeName: "+reader.getNodeName());
- method.setValue(value);
- return method;
- }
- }</span>
测试代码:
1. Object生成XML
xStreanTest.java
- <span style="font-family:Courier New;font-size:12px;"> import java.util.ArrayList;
- import java.util.List;
- import com.thoughtworks.xstream.XStream;
- public class xStreanTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- GFRep gfrep = new GFRep("test");
- Param param = new Param("location","validate method");
- Parameters p = new Parameters(param);
- List<GFContext> ls = new ArrayList<GFContext>();
- GFContext gfcontext = new GFContext("nav","gf0001",gfrep,new Method("method","http://fdsf...."),p);
- ls.add(gfcontext);
- gfrep = new GFRep("test2");
- param = new Param("location2","validate method2");
- p = new Parameters(param);
- gfcontext = new GFContext("nav","gf0002",new Method("method2",p);
- ls.add(gfcontext);
- gfrep = new GFRep("test3");
- param = new Param("location3","validate method3");
- p = new Parameters(param);
- gfcontext = new GFContext("nav","gf0003",new Method("method3",p);
- ls.add(gfcontext);
- //GuidedFlows gf = new GuidedFlows(ls);
- Banner banner = new Banner("repname");
- MainContext mc = new MainContext("ctx000001",banner,ls);
- UserDetails ud = new UserDetails("guest","JLJE89dd8DasGds6es","starhub");
- MLoginIR mir = new MLoginIR(ud,mc);
- XStream xstream = new XStream();
- xstream.registerConverter(new MethodConverter());
- xstream.alias("IR",MLoginIR.class);
- xstream.alias("UserDetails",UserDetails.class);
- xstream.alias("MainContext",MainContext.class);
- xstream.alias("Banner",Banner.class);
- //xstream.alias("GuidedFlows",GuidedFlows.class);
- xstream.alias("GFContext",GFContext.class);
- xstream.alias("GFRep",GFRep.class);
- xstream.alias("Parameters",Parameters.class);
- xstream.alias("Param",Param.class);
- //xstream.alias("RepName",RepName.class);
- xstream.aliasField("GuidedFlows",MainContext.class,"GFContext");
- xstream.useAttributeFor(GFContext.class,"GFType");
- xstream.useAttributeFor(Method.class,"Action");
- String xml = xstream.toXML(mir);
- System.out.println(xml);
- MLoginIR m = (MLoginIR)xstream.fromXML(xml);
- System.out.println(m);
- }
- }</span>
2. XML转Object
xStreanTest2.java
- <span style="font-family:Courier New;font-size:12px;">import java.util.ArrayList;
- import java.util.List;
- import com.thoughtworks.xstream.XStream;
- public class xStreanTest2 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- String xml = "TODO";
- XStream xstream = new XStream();
- xstream.registerConverter(new MethodConverter());
- xstream.alias("IR",MLoginIR.class);
- xstream.alias("UserDetails",UserDetails.class);
- xstream.alias("MainContext",MainContext.class);
- xstream.alias("Banner",Banner.class);
- xstream.alias("GFContext",GFContext.class);
- xstream.alias("GFRep",GFRep.class);
- xstream.alias("Parameters",Parameters.class);
- xstream.alias("Param",Param.class);
- //xstream.alias("RepName",RepName.class);
- xstream.alias("Method",Method.class);
- xstream.aliasField("GuidedFlows","GFContext");
- xstream.useAttributeFor(GFContext.class,"GFType");
- xstream.useAttributeFor(Method.class,"Action");
- MLoginIR newIR = (MLoginIR)xstream.fromXML(xml);
- System.out.println(newIR.getUserDetails());
- List<GFContext> ls_gf = newIR.getMainContext().getGFContext();
- Banner banner = newIR.getMainContext().getBanner();
- System.out.println(banner);
- for(GFContext gf : ls_gf){
- System.out.println("gf:"+gf);
- GFRep gfrep = gf.getGFRep();
- System.out.println(gfrep);
- Parameters p = gf.getParameters();
- System.out.println(p);
- Param p2 = p.getParam();
- System.out.println(p2);
- Method m = gf.getMethod();
- System.out.println(m);
- System.out.println("==================================");
- }
- }
- } </span>