users@jaxb.java.net

Re: AW: Re: SAX-like JAXB?

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Tue, 29 Aug 2006 16:03:11 -0700

Ernst, Matthias wrote:
>> Would you be interested in turning something like that into a
>> complete example that we can ship?
>
> Kohsuke,
>
> I've created a new sample "streaming-unmarshal" out of the
> partial-unmarshal sample.
>
> I had first hoped that I would not need to modify the generated classes
> but there is no setter for the PurchaseOrder list. Having to modify
> PurchaseOrders anyway, I created a small listener interface that is
> bridged to List#add.

I see. I think just showing this technique for 'start from Java' case is
still useful, so I don't mind you modifying the generated code.

But if you really want to avoid touching the generated code (to make the
technique applicable to schema->Java direction), then there might be a
few things we can do, because we can tell XJC to use a collection type
of your choosing. Let me play with it a bit.

Also, is there any reason you chose to create XMLReader by yourself? The
following code can be as simple as

> // create a new XML parser
> SAXParserFactory factory = SAXParserFactory.newInstance();
> factory.setNamespaceAware(true);
> XMLReader reader = factory.newSAXParser().getXMLReader();
> reader.setContentHandler(unmarshaller.getUnmarshallerHandler());
>
> for (String arg : args) {
> // parse all the documents specified via the command line.
> // note that XMLReader expects an URL, not a file name.
> // so we need conversion.
> reader.parse(new File(arg).toURI().toString());
> }

         for (String arg : args) {
           unmarshaller.unmarshal(new File(arg));
         }


> I find I still need to use the Unmarshaller.Listener since I typically
> need access to an object from "outside" within the notification handler.
> This sample only uses System.out.println but in reality I would want to
> store the PO in a database, so I need access to the datasource in the
> listener. That is not possible using the per-class listener methods
> unless they reach out and read a static/thread-local/JNDI. It think it
> is better to inject instead.

I see. What if we let you unmarshal into an existing instance of
PurchaseOrders object? (As opposed to have the JAXB RI allocate a new
instance.)

That's a useful feature on its own, and in this particular case you can
use that to your advantage (that only works for the root object, though.)

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com