users@jaxb.java.net

Re: SAX-like JAXB?

From: Ernst, Matthias <matthias.ernst_at_coremedia.com>
Date: Mon, 28 Aug 2006 13:30:43 +0200

Henning Storhaug wrote:
> I must write a xml parser that reads records and process them
individually due to memory
> constraints. I cannot read the whole file into memory. Second, the
schema is complex so I'd like
> the binding and unmarshalling benefits of JAXB.
  
This is a rather old thread
(https://jaxb.dev.java.net/servlets/BrowseList?list=users&by=thread&from
=461758); I happened to run into the same situation.

What I'm doing is the following: in the Unmarshaller.Listener I override
the list in the parent object with an implementation that calls a
"newChildUnmarshalled" handler on #add but doesn't remember the child:

(purchase order example:)

unmarshaller.setListener(new Unmarshaller.Listener() {
  public void beforeUnmarshal(Object target, Object parent) {
    if (target instanceof PurchaseOrders) {
      ((PurchaseOrders) target).setPurchaseOrders(new
ArrayList<PurchaseOrder>() {
        public boolean add(PurchaseOrder po) {
            handleNewPurchaseOrder(po); // could pass purchaseorders as
well.
          return false;
        }
      });
    }
  }
  public void beforeUnmarshal(Object target, Object parent) {
    if (target instanceof PurchaseOrders) {
      ((PurchaseOrders) target).setPurchaseOrders(null); // or whatever
    }
  }
});

I find this much more elegant than fiddling with the StAX stream or the
partial-unmarshalling sample.

Cheers
Matthias

-- 
Matthias Ernst
Software Engineer
 
tel +49.40.32 55 87.503
fax +49.40.32 55 87.999
matthias.ernst_at_coremedia.com