Guest User

Untitled

a guest
Jan 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. <Objects>
  2. <Object Type="System.Management.Automation.Internal.Host.InternalHost">
  3. <Property Name="Name" Type="System.String">ConsoleHost</Property>
  4. <Property Name="Version" Type="System.Version">2.0</Property>
  5. <Property Name="InstanceId" Type="System.Guid">7e2156</Property>
  6. </Object>
  7. </Objects>
  8.  
  9. class ParentResponseObject {
  10. List <ResponseObject>responseObjects = new ArrayList<ResponseObject>();
  11.  
  12. }
  13.  
  14. @XStreamAlias("Object")
  15. @XStreamConverter(value = ToAttributedValueConverter.class, strings = { "Value" })
  16. class ResponseObject {
  17. String Type;
  18. String Value;
  19.  
  20. List <Properties> properties = new ArrayList<Properties>();
  21. }
  22.  
  23. @XStreamAlias("Property")
  24. @XStreamConverter(value = ToAttributedValueConverter.class, strings = { "Value" })
  25. class Properties {
  26. String Name;
  27. String Type;
  28. String Value;
  29. }
  30. public class MyAgainTest {
  31. public static void main (String[] args) throws Exception {
  32. String k1 = //collect the xml as string
  33. XStream s = new XStream(new DomDriver());
  34. s.alias("Objects", ParentResponseObject.class);
  35. s.alias("Object", ResponseObject.class);
  36. s.alias("Property", Properties.class);
  37. s.useAttributeFor(ResponseObject.class, "Type");
  38. s.addImplicitCollection(ParentResponseObject.class, "responseObjects");
  39. s.addImplicitCollection(ResponseObject.class, "properties");
  40. s.useAttributeFor(Properties.class, "Name");
  41. s.useAttributeFor(Properties.class, "Type");
  42. s.processAnnotations(ParentResponseObject.class);
  43. ParentResponseObject gh =(ParentResponseObject)s.fromXML(k1);
  44. System.out.println(gh.toString());
  45. }
  46. }
  47.  
  48. @XStreamAlias("Objects")
  49. public class ParentResponseObject {
  50.  
  51. @XStreamImplicit
  52. List<ResponseObject> responseObjects = new ArrayList<ResponseObject>();
  53. }
  54.  
  55. @XStreamAlias("Object")
  56. public class ResponseObject {
  57.  
  58. @XStreamAsAttribute
  59. String Type;
  60.  
  61. @XStreamImplicit
  62. List<Properties> properties = new ArrayList<Properties>();
  63. }
  64.  
  65. @XStreamAlias("Property")
  66. @XStreamConverter(value = ToAttributedValueConverter.class, strings = { "Value" })
  67. public class Properties {
  68. String Name;
  69. String Type;
  70. String Value;
  71. }
  72.  
  73. XStream s = new XStream(new DomDriver());
  74. s.processAnnotations(ParentResponseObject.class);
  75. ParentResponseObject gh = (ParentResponseObject) s.fromXML(xml);
  76.  
  77. for (ResponseObject o : gh.responseObjects) {
  78. System.out.println(o.Type);
  79. for (Properties p : o.properties) {
  80. System.out.println(p.Name + ":" + p.Type + ":" + p.Value);
  81. }
  82. }
Add Comment
Please, Sign In to add comment