users@jaxb.java.net

Re: Marshalling in chunks?

From: <eguy66_at_optonline.net>
Date: Tue, 16 Aug 2005 16:04:03 -0400

Hi,

I tried the approach suggested for jaxb 1.0, but I think I'm doing something wrong.  Below is my sample code.  The startDocument and startElement calls seem to execute ok, but when I execute the first marshal statement, it embeds a second xml declaration inside the policies element, marshals out the jaxb data, and closes the properties element.  The second marshall statement produces another xml declaration.  The endElement statement generates an EmptyStackException, since </properties> has already been output.  I thought setting the xmlDeclaration property to false might solve things, but it had no effect.  Any suggestions?

TIA,

Ed

 

Here is the output:

Generated document:

<?xml version='1.0' encoding='utf-8'?><policies<?xml version='1.0' encoding='utf-8'?>><Marsh:MasterClass xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1.6.0/xml/" xmlns:Marsh="http://www.marsh.com/"><CoverageCd>MASTER_CLASS_ACC</CoverageCd><CoverageDesc>Master Class CovDesc</CoverageDesc><ClaimsMadeInd>0</ClaimsMadeInd><Marsh:MajorClass><CoverageCd>MAJOR_CLASS_5AA</CoverageCd><CoverageDesc>Accidental Medical A D &amp; D</CoverageDesc><Marsh:MinorClass><CoverageCd>DETAIL_CLASS_ACC_ADD</CoverageCd><CoverageDesc>Accidental Death/Dismemberment</CoverageDesc><Marsh:MarshOption><OptionTypeCd>COV_PROP_TYPE_COVVAR</OptionTypeCd><Marsh:MarshOptionValue>100</Marsh:MarshOptionValue><Marsh:OptionUnit>THOUSAND</Marsh:OptionUnit></Marsh:MarshOption><Marsh:MarshOption><OptionTypeCd>COV_PROP_TYPE_COVVAR</OptionTypeCd><Marsh:MarshOptionValue>10</Marsh:MarshOptionValue><Marsh:OptionUnit>THOUSAND</Marsh:OptionUnit></Marsh:MarshOption></Marsh:MinorClass></Marsh:MajorClass></Marsh:MasterClass></policies><?xml version='1.0' encoding='utf-8'?><Ma rsh:MasterClass xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1.6.0/xml/" xmlns:Marsh="http://www.marsh.com/"><CoverageCd>MASTER_CLASS_ACC</CoverageCd><CoverageDesc>Master Class CovDesc</CoverageDesc><ClaimsMadeInd>0</ClaimsMadeInd><Marsh:MajorClass><CoverageCd>MAJOR_CLASS_5AA</CoverageCd><CoverageDesc>Accidental Medical A D &amp; D</CoverageDesc><Marsh:MinorClass><CoverageCd>DETAIL_CLASS_ACC_ADD</CoverageCd><CoverageDesc>Accidental Death/Dismemberment</CoverageDesc><Marsh:MarshOption><OptionTypeCd>COV_PROP_TYPE_COVVAR</OptionTypeCd><Marsh:MarshOptionValue>100</Marsh:MarshOptionValue><Marsh:OptionUnit>THOUSAND</Marsh:OptionUnit></Marsh:MarshOption><Marsh:MarshOption><OptionTypeCd>COV_PROP_TYPE_COVVAR</OptionTypeCd><Marsh:MarshOptionValue>10</Marsh:MarshOptionValue><Marsh:OptionUnit>THOUSAND</Marsh:OptionUnit></Marsh:MarshOption></Marsh:MinorClass></Marsh:MajorClass></Marsh:MasterClass>java.util.EmptyStackException

 at com.bea.xml.stream.util.Stack.pop(Stack.java:82)

 at com.bea.xml.stream.XMLWriterBase.writeEndElement(XMLWriterBase.java:367)

 at javanet.staxutils.ContentHandlerToXMLStreamWriter.endElement(ContentHandlerToXMLStreamWriter.java:210)

 at com.marsh.jaxtest.Test.main(JAXBTest.java:1912)

 at com.marsh.jaxtest.JAXBTest.main(JAXBTest.java:179)

Exception in thread "main"

 

class Test {


public static void main(String[] args) throws Exception {


JAXBContext jc = JAXBContext.newInstance("com.marsh.common.jaxb.acord");
Unmarshaller u = jc.createUnmarshaller();
Marshaller m = jc.createMarshaller();
ObjectFactory objFactory = new ObjectFactory();
m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
// m.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

XMLStreamWriter xw = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
ContentHandlerToXMLStreamWriter ch = new ContentHandlerToXMLStreamWriter(xw);

System.out.println("\nGenerated document:");
ch.startDocument();
ch.startElement("","policies","",new AttributesImpl());
m.marshal(JAXBTest.generateMasterClass(objFactory), ch);
m.marshal(JAXBTest.generateMasterClass(objFactory), ch);
ch.endElement("","policies","");
ch.endDocument();


}


}

 

----- Original Message -----
From  Kohsuke Kawaguchi <Kohsuke.Kawaguchi@Sun.COM>
Date  Thu, 11 Aug 2005 13:01:24 -0700
To  users@jaxb.dev.java.net
Subject  Re: Marshalling in chunks?


eguy66@optonline.net wrote:
> I realize I can marshall each policy item separately, but is there a way
> to marshall just the start tag of <policies>, marshall each individual
> policy item and then marshall the ending </policies> tag (so that I have
> a complete, valid xml document), without building and keeping all
> policies in memory as a big jaxb collection, and marshalling the entire
> object graph?  I'm looking for the logical inverse of the
> Partial-Unmarshalling sample, which works very well for the requestor
> receiving this information.

If you are using JAXB 2.0, this is most easily done by using StAX. You
invoke StAX directly to write <policies>, then you pass that StAX writer
and marshal a policy object individually. You then conclude again by
writing </policies> by using the StAX writer directly. I believe there's
a sample bundled with the distribution package that does this.

With JAXB 1.0, you can still do this, but JAXB 1.0 marshaller doesn't
take StAX writer directly, so you need an adapter to bridge them
together. Look at http://stax-utils.dev.java.net/ and use its
ContentHandler->XMLStreamWriter adapter to do this.


--
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi@sun.com