Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] StaX/DOM/SAX-to-XOM bridge

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Elliotte Rusty Harold <elharo AT ibiblio.org>
  • To: XOM API for Processing XML with Java <xom-interest AT lists.ibiblio.org>
  • Subject: Re: [XOM-interest] StaX/DOM/SAX-to-XOM bridge
  • Date: Wed, 15 Sep 2010 07:13:34 -0400

On Tue, Sep 14, 2010 at 11:11 AM, Dmitry Katsubo <dma_k AT mail.ru> wrote:
> Dear XOM users!
>
> I want to benefit from quick StaX/DOM/SAX -to- XOM document creation,
> but I was not able to locate any way to do so. Actually, my target API I
> want to bind with is javax.xml.bind.Marshaller.marshal(), that accepts
> javax.xml.transform.Result, org.xml.sax.ContentHandler or
> javax.xml.stream.XMLStreamWriter as possible outputs.
>

SAX and DOM are supported. StAX and Trax are not. StAX never really
took off, and Trax is severely underspecified. It's a classic example
of how not to do inheritance. Essentialy the supertypes
javax.xml.transform.Source and javax.xml.transform.Result are
useless. You have to cast to the subtypes and then use their methods
to get anything done, and you have to know the subtypes you support in
advance.

What you need to do is marshal straight to an output stream that is
fed into a Builder. Something like

ByteArrayOutputStream out = new ByteArrayOutputStream();
marshaller.marshal(bean, out);
out.close();
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
Builder builder = new Builder();
nu.xom.Document doc = builder.build(in);

You could even do this with piped streams in two threads if you're
concerned about the memory footprint of serializing the entire
document into a string.

If this were a common need, I could think about adding support for
doing this directly through a ContentHandler. off the top of my head,
I think you're the first person to ask for it, and JAXB is the first
API I've noticed that used content handlers like this. Usually I look
for at least two different use cases from two or more people before
adding something to the API.

--
Elliotte Rusty Harold
elharo AT ibiblio.org




Archive powered by MHonArc 2.6.24.

Top of Page