Skip to content
Advertisement

Get progress information during JAXB de-/serialization

Is there a way to register some progress monitor on JAXB Marshaller and Unmarshaller? I would like to show some progress information in my GUI while data is de-/serialized.

I see that you can set a Unmarshaller.Listener and Marshaller.Listener, which have a “before” and “after” method. Nevertheless, I do not see any straight forward way to get the total number of elements to serialize.

I would need that obviously to calculate some “percentage done” info.

Advertisement

Answer

Is it ok to parse before unmarshalling?

If so, assuming you have a list of objects, you could do something like…

    final String tagName = *** name of tag you are counting ***;
    InputStream in = *** stream of your xml ***;

    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser saxParser = spf.newSAXParser();
    final AtomicInteger counter = new AtomicInteger();
    saxParser.parse(in, new DefaultHandler() {
        @Override
        public void startElement (String uri, String localName, String qName, Attributes attributes) {
            if (localName.equals(tagName))
                counter.incrementAndGet();
        }
    });
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement