users@jaxb.java.net

Re: how to marshal an object to document fragment

From: Gary Gregory <ggregory_at_seagullsw.com>
Date: Tue, 28 Jan 2003 11:02:23 -0500

Kohsuke,

Thank you for clearly outlining a solution.

At the risk of sounding ungrateful... isn't there a JAXP-only solution to
this? We have cleaned up our code base away from Xerces with JAXP, and
eventhough we do deliver Xerces with our server we would like to keep a
clean JAXP based solution if possible to allow us not to deliver Xerces
when/if we switch from moving our platform requirement from JRE 1.3.x to
1.4.x. Ideally, we would like customers/consultants to be able to plug-in
their JAXP compliant parsers/transformers.

Thank you,
Gary

-----Original Message-----
From: Kohsuke Kawaguchi [mailto:Kohsuke.Kawaguchi_at_Sun.COM]
Sent: Tuesday, January 28, 2003 7:34 AM
To: JAXB-INTEREST_at_JAVA.SUN.COM
Subject: Re: how to marshal an object to document fragment


> Actually, how would one simply strip the <?xml ...?> PI?

Whenever you'd like to modify the marshalling result, do as follows:

1. Look for XML writer implementation that fits your needs. Major ones
   are:

   a. Apache XMLSerializer
   b. David Megginson's XMLWriter (http://www.megginson.com/Software/)
   c. XmlWriter in GNU JAXP

   but there are a plenty of others. Just try to google the web. Choose
   one that can write SAX events.

2. Let's say we use Apache XMLWriter. Their documentation shows how to
   configure it to strip the xml declaration. In their case, it would
   be:

   OutputFormat of = new OutputFormat();
   of.setOmitXMLDeclaration(true);

   XMLSerializer xs = new XMLSerializer(new FileOutputStream("abc.xml"), of
);

3. Then pass this serializer object as a parameter to the marshaller.

   marshaller.marshal( jaxbObject, xs );


You can apply this technique to various other things.

regards,
--
Kohsuke KAWAGUCHI                  408-276-7063 (x17063)
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com