users@jaxb.java.net

Re: equivalent of XMLBeans XMLObject interface

From: Kohsuke Kawaguchi <kohsuke.kawaguchi_at_sun.com>
Date: Thu, 27 Oct 2005 13:09:30 -0700

Here's how you do it. Since JAXB has to support POJOs, it's difficult to
defint those as methods on objects. So they are defined on Marshaller.

Mark Hansen wrote:
> newDomNode() - to get an instance of the XML as a DOM

DOMResult dr = new DOMResult();
marshaller.marshal( object, dr );
dr.getNode()

> newInputStream() - to get it as a stream

ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal( object, baos );
new ByteArrayInputStream(baos.toByteArray());

(On this one, XMLBeans implement it better, as it doesn't have to buffer
things)

> xmlText() - to get it as a String

StringWriter sw = new StringWriter();
marshaller.marshal( object, sw );
sw.toString();

> save(ContentHandler ch, LexicalHandler hl) - to run the instance through
> SAX handlers

SAXResult sr = new SAXResult(...);
marshaller.marshal( object, sr );


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com