Skip to main content

SAX Parsing and JAXB

No replies
lingjo
Offline
Joined: 2007-08-06

Hello,

I'm trying to parse an XML document using SAX and unmarshalling it into a JAXB generated objects.

I'm trying to follow the example outlined in the JAXB API documentation below.

Could someone please clarify how one checks for the events? When I try a specific example, it never seems to catch an event as I find that vec.hasEvents() is always false.

Also, if my xml source is very large then doing something like:
Object o = u.unmarshal( source ); then the JVM will run out of memory, which defeats the purpose of SAX.

Sample:

XMLReader xmlReader = saxParser.getXMLReader();
SAXSource source = new SAXSource( xmlReader, new InputSource( "http://..." ) );

// Setup JAXB to unmarshal
JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );
Unmarshaller u = jc.createUnmarshaller();
ValidationEventCollector vec = new ValidationEventCollector();
u.setEventHandler( vec );

// turn off the JAXB provider's default validation mechanism to
// avoid duplicate validation
u.setValidating( false )

// unmarshal
Object o = u.unmarshal( source );

// check for events
if( vec.hasEvents() ) {
// iterate over events
}

Thanks,
John