users@jaxb.java.net

How to transform Element to String [WAS: Re: How to use anytype: get and set value in pojo]

From: Martin Grotzke <martin.grotzke_at_javakaffee.de>
Date: Tue, 19 Feb 2008 23:17:41 +0100

Hi,

just reposting the second part of my issue:

XJC generates an @XmlAnyElement Element, which is what I want. Now I'm
left with the question, what's the best way to read the element to s.th.
I could store in the database (e.g. a string), and how to recreate an
Element from e.g. a string.

Is it good (in terms of performance, simplicity) to do this by using
Transformer, DOMSource and StringWrite/StreamResult like this:

        final DOMSource source = new DOMSource( any );
        final Transformer transformer =
TransformerFactory.newInstance().newTransformer();
        final StringWriter stringWriter = new StringWriter();
        final StreamResult result = new StreamResult( stringWriter );
        transformer.transform( source, result );

Or is there an easier way?

Thanx && cheers,
Martin


On Tue, 2008-02-12 at 09:43 +0100, Martin Grotzke wrote:
> Hi,
>
> now I found this in the unofficial user guide, wich explains the first
> part of my issue:
> https://jaxb.dev.java.net/guide/Mapping_of__xs_any___.html
>
> So I have changed my schema to this:
>
> <element name="myDocument">
> <complexType>
> <all>
> <element name="id" type="string" />
> <element name="data" minOccurs="0">
> <complexType>
> <any processContents="skip" />
> </complexType>
> </element>
> </all>
> </complexType>
> </element>
>
> XJC generates an @XmlAnyElement Element for this, which is probably what
> I want. Now I'm left with the question, what's the best way to read the
> element to s.th. I could store in the database (e.g. a string), and how
> to recreate an Element from e.g. a string.
>
> Is it good (in terms of performance, simplicity) to do this by using
> Transformer, DOMSource and StringWrite/StreamResult like this:
>
> final DOMSource source = new DOMSource( any );
> final Transformer transformer = TransformerFactory.newInstance().newTransformer();
> final StringWriter stringWriter = new StringWriter();
> final StreamResult result = new StreamResult( stringWriter );
> transformer.transform( source, result );
>
> Or is there an easier way?
>
> Thanx && cheers,
> Martin
>
>
> On Mon, 2008-02-11 at 21:55 +0000, Kenny MacLeod wrote:
> > Have you tried to see what XJC generates from your schema?
> >
> > Martin Grotzke wrote:
> > > Hello,
> > >
> > > I have an xmldocument with one element that is of xsd:anyType (the user
> > > must be able to store any xml structure within this element):
> > >
> > > <element name="myDocument">
> > > <complexType>
> > > <all>
> > > <element name="id" type="string" />
> > > <element name="myData" type="anyType" />
> > > </all>
> > > </complexType>
> > > </element>
> > >
> > > I want to read the content of myData when a user submits a document and
> > > store this content in the database. Later, when the user asks me for
> > > this document, I want to create it and set the content of myData to the
> > > previously stored content.
> > >
> > > The pojos are created from a schema (using xjc).
> > >
> > > What's the best way to achieve this in jaxb?
> > >
> > > Thanx a lot in advance,
> > > cheers,
> > > Martin
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> > For additional commands, e-mail: users-help_at_jaxb.dev.java.net
> >