How can I export or save a inspected object structure in eclipse?

4.8k views Asked by At

Is there a way to save/export (also need to be able to view later) an inspected object structure?

sample inspection window in eclipse

Possibly export to a XML or JSON structure?

2

There are 2 answers

1
tostao On

You can use xstream, e.g.

Java objects:

 public class Person {
      private String firstname;
      private String lastname;
      private PhoneNumber phone;
      private PhoneNumber fax;
      // ... constructors and methods
    }

public class PhoneNumber {
  private int code;
  private String number;
  // ... constructors and methods
}'

Simply instantiate the XStream class:

XStream xstream = new XStream();

Create an instance of Person and populate its fields:

Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));

Convert it to XML

String xml = xstream.toXML(joe);'

Result

<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>
0
Mistriel On

I think tostao reply is correct, just create a helper class with static method that you will call during debugging that will persist the XML to some file on the system .

Then use Eclipse "expressions" view to execute the command. E.g :

FileUtils.persistObjToXml(obj,path)