使用XStream解析复杂的XML文件

前端之家收集整理的这篇文章主要介绍了使用XStream解析复杂的XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
XStream在实现Java、xml之间转换非常的出色。但是有时候一些比较复杂的XML文件就不好下手,在此以如下XML为例演示了XML to Object,Object to XML的过程。

依赖包:
xstream-1.3.1.jar
xpp3_min-1.1.4c.jar

XML文件

  1. <IR>
  2. <UserDetails>
  3. <UID>guest</UID>
  4. <UType>JLJE89dd8DasGds6es</UType>
  5. <UToken>DG8kxkSEO903kdm8e2j4Kds8d</UToken>
  6. </UserDetails>
  7. <MainContext>
  8. <CID>ctx000001</CID>
  9. <Banner>
  10. <RepName>repname</RepName>
  11. </Banner>
  12. <GuidedFlows>
  13. <GFContext GFType="nav">
  14. <GFCID>gf0001</GFCID>
  15. <GFRep>
  16. <RepName>test</RepName>
  17. </GFRep>
  18. <Method Action="method">http://fdsf....</Method>
  19. <Parameters>
  20. <Param>
  21. <Location>location</Location>
  22. <ValidateMethod>validate method</ValidateMethod>
  23. </Param>
  24. </Parameters>
  25. </GFContext>
  26. <GFContext GFType="nav">
  27. <GFCID>gf0002</GFCID>
  28. <GFRep>
  29. <RepName>test2</RepName>
  30. </GFRep>
  31. <Method Action="method2">http://fdsf....</Method>
  32. <Parameters>
  33. <Param>
  34. <Location>location2</Location>
  35. <ValidateMethod>validate method2</ValidateMethod>
  36. </Param>
  37. </Parameters>
  38. </GFContext>
  39. <GFContext GFType="nav">
  40. <GFCID>gf0003</GFCID>
  41. <GFRep>
  42. <RepName>test3</RepName>
  43. </GFRep>
  44. <Method Action="method3">http://fdsf....</Method>
  45. <Parameters>
  46. <Param>
  47. <Location>location3</Location>
  48. <ValidateMethod>validate method3</ValidateMethod>
  49. </Param>
  50. </Parameters>
  51. </GFContext>
  52. </GuidedFlows>
  53. </MainContext>
  54. </IR>
定义实体类:

UserDetails:

  1. public class UserDetails {
  2. private String UID;
  3. private String UType;
  4. private String UToken;
  5. public UserDetails(String UID,String UType,String uToken) {
  6. this.UID = UID;
  7. this.UType = UType;
  8. this.UToken = uToken;
  9. }
  10. public String getUID() {
  11. return UID;
  12. }
  13. public void setUID(String uID) {
  14. UID = uID;
  15. }
  16. public String getUType() {
  17. return UType;
  18. }
  19. public void setUType(String uType) {
  20. UType = uType;
  21. }
  22. public String getUToken() {
  23. return UToken;
  24. }
  25. public void setUToken(String uToken) {
  26. UToken = uToken;
  27. }
  28. @Override
  29. public String toString() {
  30. return "UserDetails [UID=" + UID + ",UType=" + UType + ",UToken="
  31. + UToken + "]";
  32. }
  33. }

MainContext:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class MainContext {
  4. private String CID;
  5. private Banner Banner;
  6. private List<GFContext> GFContext = new ArrayList<GFContext>();
  7. public MainContext(String CID,Banner Banner,List<GFContext> GFContext) {
  8. this.CID = CID;
  9. this.Banner = Banner;
  10. this.GFContext = GFContext;
  11. }
  12. public String getCID() {
  13. return CID;
  14. }
  15. public void setCID(String cID) {
  16. CID = cID;
  17. }
  18. public Banner getBanner() {
  19. return Banner;
  20. }
  21. public void setBanner(Banner banner) {
  22. Banner = banner;
  23. }
  24. public List<GFContext> getGFContext() {
  25. return GFContext;
  26. }
  27. public void setGFContext(List<GFContext> gFContext) {
  28. GFContext = gFContext;
  29. }
  30. @Override
  31. public String toString() {
  32. return "MainContext [CID=" + CID + ",Banner=" + Banner
  33. + ",GuidedFlows=" + GFContext + "]";
  34. }
  35. }

Banner:

  1. public class Banner {
  2. private String RepName;
  3. public Banner(String RepName){
  4. this.RepName = RepName;
  5. }
  6. public String getRepName() {
  7. return RepName;
  8. }
  9. public void setRepName(String repName) {
  10. RepName = repName;
  11. }
  12. @Override
  13. public String toString() {
  14. return "Banner [RepName=" + RepName + "]";
  15. }
  16. }

GFContext:

  1. public class GFContext {
  2. private String GFType;
  3. private String GFCID;
  4. private GFRep GFRep;
  5. private Method Method;
  6. private Parameters Parameters;
  7. public GFContext(String GFType,String GFCID,GFRep GFRep,Method Method,Parameters Parameters){
  8. this.GFType = GFType;
  9. this.GFCID = GFCID;
  10. this.GFRep = GFRep;
  11. this.Method = Method;
  12. this.Parameters = Parameters;
  13. }
  14. public String getGFType() {
  15. return GFType;
  16. }
  17. public void setGFType(String gFType) {
  18. this.GFType = gFType;
  19. }
  20. public String getGFCID() {
  21. return GFCID;
  22. }
  23. public void setGFCID(String gFCID) {
  24. this.GFCID = gFCID;
  25. }
  26. public GFRep getGFRep() {
  27. return GFRep;
  28. }
  29. public void setGFRep(GFRep gFRep) {
  30. this.GFRep = gFRep;
  31. }
  32. public Method getMethod() {
  33. return Method;
  34. }
  35. public void setMethod(Method Method) {
  36. this.Method = Method;
  37. }
  38. public Parameters getParameters() {
  39. return Parameters;
  40. }
  41. public void setParameters(Parameters parameters) {
  42. this.Parameters = parameters;
  43. }
  44. @Override
  45. public String toString() {
  46. return "GFContext [GFType=" + GFType + ",GFCID=" + GFCID + ",GFRep="
  47. + GFRep + ",Method=" + Method + ",Parameters=" + Parameters
  48. + "]";
  49. }
  50. }

GFRep:

  1. public class GFRep {
  2. private String RepName;
  3. private String RepURL;
  4. public GFRep(String RepName) {
  5. this.RepName = RepName;
  6. }
  7. public GFRep(String RepName,String RepURL) {
  8. this.RepName = RepName;
  9. this.RepURL = RepURL;
  10. }
  11. public String getRepName() {
  12. return RepName;
  13. }
  14. public void setRepName(String repName) {
  15. RepName = repName;
  16. }
  17. public String getRepURL() {
  18. return RepURL;
  19. }
  20. public void setRepURL(String repURL) {
  21. RepURL = repURL;
  22. }
  23. @Override
  24. public String toString() {
  25. return "GFRep [RepName=" + RepName + ",RepURL=" + RepURL + "]";
  26. }
  27. }

Method:

  1. public class Method {
  2. private String Action;
  3. private String value;
  4. public Method(){
  5. }
  6. public Method(String value){
  7. this.value = value;
  8. }
  9. public Method(String Action,String value){
  10. this.Action = Action;
  11. this.value = value;
  12. }
  13. public String getAction() {
  14. return Action;
  15. }
  16. public void setAction(String action) {
  17. Action = action;
  18. }
  19. public String getValue() {
  20. return value;
  21. }
  22. public void setValue(String value) {
  23. this.value = value;
  24. }
  25. @Override
  26. public String toString() {
  27. return "Method [Action=" + Action + ",value=" + value + "]";
  28. }
  29. }

Parameters:

  1. public class Parameters {
  2. private Param Param;
  3. public Parameters(Param Param) {
  4. this.Param = Param;
  5. }
  6. public Param getParam() {
  7. return Param;
  8. }
  9. public void setParam(Param param) {
  10. Param = param;
  11. }
  12. @Override
  13. public String toString() {
  14. return "Parameters [Param=" + Param + "]";
  15. }
  16. }


Param:

  1. <span style="font-family:Courier New;font-size:12px;">public class Param {
  2. private String Location;
  3. private String ValidateMethod;
  4. public Param(String Location) {
  5. this.Location = Location;
  6. }
  7. public Param(String Location,String ValidateMethod) {
  8. this.Location = Location;
  9. this.ValidateMethod = ValidateMethod;
  10. }
  11. public String getLocation() {
  12. return Location;
  13. }
  14. public void setLocation(String location) {
  15. Location = location;
  16. }
  17. public String getValidateMethod() {
  18. return ValidateMethod;
  19. }
  20. public void setValidateMethod(String validateMethod) {
  21. ValidateMethod = validateMethod;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Param [location=" + Location + ",validateMethod="
  26. + ValidateMethod + "]";
  27. }
  28. }</span>
由于我们需要实现类似:<field name="value">I am a Field.</Method>这样的效果。因此,我们需要实现一个Converter。这个很关键,也是我试了好久才解决的问题,在此也分享一下:

MethodConverter.java:


  1. <span style="font-family:Courier New;font-size:12px;">import com.thoughtworks.xstream.converters.Converter;
  2. import com.thoughtworks.xstream.converters.MarshallingContext;
  3. import com.thoughtworks.xstream.converters.UnmarshallingContext;
  4. import com.thoughtworks.xstream.io.HierarchicalStreamReader;
  5. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
  6. public class MethodConverter implements Converter {
  7. public boolean canConvert(Class clazz) {
  8. return clazz.equals(Method.class);
  9. }
  10. public void marshal(Object value,HierarchicalStreamWriter writer,MarshallingContext context) {
  11. Method method = (Method)value;
  12. if (method!=null) {
  13. writer.addAttribute("Action",method.getAction());
  14. writer.setValue(method.getValue());
  15. }
  16. }
  17. @Override
  18. public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
  19. Method method = new Method();
  20. String action = reader.getAttribute("Action");
  21. method.setAction(action);
  22. System.out.println(">>> Action: "+action);
  23. String value = reader.getValue();
  24. System.out.println(">>> method: "+value+",NodeName: "+reader.getNodeName());
  25. method.setValue(value);
  26. return method;
  27. }
  28. }</span>

测试代码

1. Object生成XML

xStreanTest.java


  1. <span style="font-family:Courier New;font-size:12px;"> import java.util.ArrayList;
  2. import java.util.List;
  3. import com.thoughtworks.xstream.XStream;
  4. public class xStreanTest {
  5. /**
  6. * @param args
  7. */
  8. public static void main(String[] args) {
  9. GFRep gfrep = new GFRep("test");
  10. Param param = new Param("location","validate method");
  11. Parameters p = new Parameters(param);
  12. List<GFContext> ls = new ArrayList<GFContext>();
  13. GFContext gfcontext = new GFContext("nav","gf0001",gfrep,new Method("method","http://fdsf...."),p);
  14. ls.add(gfcontext);
  15. gfrep = new GFRep("test2");
  16. param = new Param("location2","validate method2");
  17. p = new Parameters(param);
  18. gfcontext = new GFContext("nav","gf0002",new Method("method2",p);
  19. ls.add(gfcontext);
  20. gfrep = new GFRep("test3");
  21. param = new Param("location3","validate method3");
  22. p = new Parameters(param);
  23. gfcontext = new GFContext("nav","gf0003",new Method("method3",p);
  24. ls.add(gfcontext);
  25. //GuidedFlows gf = new GuidedFlows(ls);
  26. Banner banner = new Banner("repname");
  27. MainContext mc = new MainContext("ctx000001",banner,ls);
  28. UserDetails ud = new UserDetails("guest","JLJE89dd8DasGds6es","starhub");
  29. MLoginIR mir = new MLoginIR(ud,mc);
  30. XStream xstream = new XStream();
  31. xstream.registerConverter(new MethodConverter());
  32. xstream.alias("IR",MLoginIR.class);
  33. xstream.alias("UserDetails",UserDetails.class);
  34. xstream.alias("MainContext",MainContext.class);
  35. xstream.alias("Banner",Banner.class);
  36. //xstream.alias("GuidedFlows",GuidedFlows.class);
  37. xstream.alias("GFContext",GFContext.class);
  38. xstream.alias("GFRep",GFRep.class);
  39. xstream.alias("Parameters",Parameters.class);
  40. xstream.alias("Param",Param.class);
  41. //xstream.alias("RepName",RepName.class);
  42. xstream.aliasField("GuidedFlows",MainContext.class,"GFContext");
  43. xstream.useAttributeFor(GFContext.class,"GFType");
  44. xstream.useAttributeFor(Method.class,"Action");
  45. String xml = xstream.toXML(mir);
  46. System.out.println(xml);
  47. MLoginIR m = (MLoginIR)xstream.fromXML(xml);
  48. System.out.println(m);
  49. }
  50. }</span>

2. XML转Object

xStreanTest2.java


  1. <span style="font-family:Courier New;font-size:12px;">import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. import com.thoughtworks.xstream.XStream;
  5.  
  6. public class xStreanTest2 {
  7.  
  8. /**
  9. * @param args
  10. */
  11. public static void main(String[] args) {
  12. String xml = "TODO";
  13. XStream xstream = new XStream();
  14. xstream.registerConverter(new MethodConverter());
  15. xstream.alias("IR",MLoginIR.class);
  16. xstream.alias("UserDetails",UserDetails.class);
  17. xstream.alias("MainContext",MainContext.class);
  18. xstream.alias("Banner",Banner.class);
  19. xstream.alias("GFContext",GFContext.class);
  20. xstream.alias("GFRep",GFRep.class);
  21. xstream.alias("Parameters",Parameters.class);
  22. xstream.alias("Param",Param.class);
  23. //xstream.alias("RepName",RepName.class);
  24. xstream.alias("Method",Method.class);
  25. xstream.aliasField("GuidedFlows","GFContext");
  26. xstream.useAttributeFor(GFContext.class,"GFType");
  27. xstream.useAttributeFor(Method.class,"Action");
  28. MLoginIR newIR = (MLoginIR)xstream.fromXML(xml);
  29. System.out.println(newIR.getUserDetails());
  30. List<GFContext> ls_gf = newIR.getMainContext().getGFContext();
  31. Banner banner = newIR.getMainContext().getBanner();
  32. System.out.println(banner);
  33. for(GFContext gf : ls_gf){
  34. System.out.println("gf:"+gf);
  35. GFRep gfrep = gf.getGFRep();
  36. System.out.println(gfrep);
  37. Parameters p = gf.getParameters();
  38. System.out.println(p);
  39. Param p2 = p.getParam();
  40. System.out.println(p2);
  41. Method m = gf.getMethod();
  42. System.out.println(m);
  43. System.out.println("==================================");
  44. }
  45. }
  46. } </span>

猜你在找的XML相关文章