users@jaxb.java.net

Re: how to marshal an object to document fragment

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Tue, 28 Jan 2003 07:33:52 -0800

> 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