schnawel007 schnawel007 - 3 months ago 20
Java Question

XStream Java display progress

is there a possibility to display the progress of the serialization/deserialization process? Because I have a huge amount of Objects (200.000) and it takes 10 to 20 seconds, so I want to show the user a progress bar. Or is there a better solution/library?

Answer

you can create a custom Converter...

i assume that you have a collection in wich resides your data...

ArrayList<String> stringList = new ArrayList<String>();  //example list
stringList.add("abc"); //example data
stringList.add("abc"); //example data
stringList.add("abc"); //example data
stringList.add("abc"); //example data

XStream xstream = new XStream();
xstream.alias("string", String.class ); //example aliasing

Mapper mapper = xstream.getMapper();

CollectionConverter aColCon = new CollectionConverter(mapper) {

    @Override
    protected void writeItem(Object item, MarshallingContext context, HierarchicalStreamWriter writer){
        super.writeItem(item, context, writer);
        System.out.println("write object item"+item);
        //TODO your progress bar here!

    }
};

xstream.registerConverter(aColCon);
String asXml = xstream.toXML(stringList);
System.out.println(asXml);

this is the output:

write object itemabc
write object itemabc
write object itemabc
write object itemabc
<list>
  <string>abc</string>
  <string>abc</string>
  <string>abc</string>
  <string>abc</string>
</list>

have a look at http://x-stream.github.io/javadoc/com/thoughtworks/xstream/converters/collections/AbstractCollectionConverter.html#readItem%28com.thoughtworks.xstream.io.HierarchicalStreamReader,%20com.thoughtworks.xstream.converters.UnmarshallingContext,%20java.lang.Object%29 there you can find you best converter...