You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by pt...@alberta.com on 2002/05/15 19:59:48 UTC

what is good way to output XML from Java objects?

Hi folks,

I was just talking to Harry Lai(a very helpful cocoon user I might add!)
about the problem of outputting XML from Java.

>From my experience, creating an XML output string from Java object is
messy work. As bad as the original problem of outputting HTML content
in Java. Ugly stuff. 

>From his experience, Harry says their group uses Velocity.
He says when they  started their current project, one of their high priority
requirements was finding a clean(er) way of building up XML content.
After some looking around, they decided to use Velocity (wrapped inside
an EJB)
to generate the XML.  And So far, he says they've been very happy with
it (and Cocoon). =) 

I also heard that Castor might be a good solution. 

Can anyone comment or suggest any other good way to transform XMl from
Java?

Thanks,
Phong

PS
I forward you our discussion just in case I left anything out


___________________________________________________________
FREE voicemail, email and fax, all in one place.  Sign Up Now! http://www.alberta.com

Re: what is good way to output XML from Java objects?

Posted by Werner Guttmann <We...@morganstanley.com>.
Phong,

I think you've got various options here (some of which I've used in the past 9 months).

a) Use a XML binding framework such as JAXB, Castor, etc. Whilst this helps you in
creating XML from a hierarchy of Java objects, most of these frameworks serialize the
object hierarchy as an XML String (which somehow needs to be reinserted into Cocoon's
processing pipeline).

b) Use one of Cocoon's standard interfaces to plug your custom components into Cocoon
through generation of SAX events. The interface I am talking about is called XMLizable
and declares the methods toSAX(DocumentHandler) - and toDOM (Element), for historic
reasons, I suppose. We, for example,  use this interface to plug a hierarchy of service
objects, business delegates, persistence layer implemented using Castor JDO, domain
objects, value objects, etc.) into Cocoon through its standard expression evaluation
mechanism in the XSP generator.

Given the same patterns are fairly common in the EJB scheme of things, I would not see a
reason why one could not apply the same principles to an EJB solution where one could
delegate transformation to SAX events to a combination of value objects as returned by
your EJB session facade and a piece of code that implements the XMLizable interface.

c) Deploy your backend objects as a set of web services and use SOAP to obtain the data.

I hope this helps.

Werner Guttmann

ptv@alberta.com wrote:

> Hi folks,
>
> I was just talking to Harry Lai(a very helpful cocoon user I might add!)
> about the problem of outputting XML from Java.
>
> From my experience, creating an XML output string from Java object is
> messy work. As bad as the original problem of outputting HTML content
> in Java. Ugly stuff.
>
> From his experience, Harry says their group uses Velocity.
> He says when they  started their current project, one of their high priority
> requirements was finding a clean(er) way of building up XML content.
> After some looking around, they decided to use Velocity (wrapped inside
> an EJB)
> to generate the XML.  And So far, he says they've been very happy with
> it (and Cocoon). =)
>
> I also heard that Castor might be a good solution.
>
> Can anyone comment or suggest any other good way to transform XMl from
> Java?
>
> Thanks,
> Phong
>
> PS
> I forward you our discussion just in case I left anything out
>
> ___________________________________________________________
> FREE voicemail, email and fax, all in one place.  Sign Up Now! http://www.alberta.com
>
>   ------------------------------------------------------------------------
>
> Subject: RE: Question about best approach to transform Java Object to XML
>      represention for use in Cocoon
> Date: Wed, 15 May 2002 12:21:43 -0500
> From: "Lai, Harry" <HL...@coremetrics.com>
> To: "'ptv@alberta.com'" <pt...@alberta.com>
>
> Hi Phong,
>
> Yeah, I totally agree with you about the ugliness of constructing XML in
> Java.  When we started our current project, one of our high priority
> requirements was finding a clean(er) way of building up XML content.  After
> some looking around, we decided to use Velocity (wrapped inside an EJB) to
> generate the XML.  If you're not familiar with Velocity, it's a
> template-based tool from Apache
> (http://jakarta.apache.org/velocity/index.html).  So far, we've been very
> happy with it (and Cocoon).  =)
>
> Harry
>
> PS  I'm not familiar with Castor, but I think there's been some discussion
> about it on the cocoon-users list.
>
> -----Original Message-----
> From: ptv@alberta.com [mailto:ptv@alberta.com]
> Sent: Wednesday, May 15, 2002 12:09 PM
> To: Lai, Harry
> Subject: RE: Question about best approach to transform Java Object to
> XML represention for use in Cocoon
>
> Hi Harry,
>
> Thanks for your quick reply!
> What you describe sounds very nice and clean. Exactly what I need.
>
> But it now leads me to this follow up question. I was hoping the Xmlizable
> interface would make it easier for the java object to output xml content.
> But from what you tell me, it looks like xmlizable doesn't help generate
> the xml at all. only how it is treated by cocoon.
>
> From my experience, creating an XML output string from Java object is
> messy work. As bad as the original problem of outputting HTML content
> in Java. Ugly stuff.
> What do you use in your java object to create the XML output?
>
> I heard Castor might be a good tool to do this.
>
> thanks again for your help.
> Phong
> xmlSource.getContent();
>
> ---- "Lai, Harry" <HL...@coremetrics.com> wrote:
> > Hi Phong,
> >
> > How's it going?  I actually found a simpler solution since I posted
> > that
> > message (thanks to a post from someone else).  It relies on the util
> > logicsheet that comes with Cocoon2.
> >
> > So as you probably found, if you just use <xsp:expr> to output xml
> > from a
> > java object, the xml gets escaped and is treated as text.  However,
> > if you
> > surround your <xsp:expr> tag with tags from the util logicsheet (example
> > below), the xml from your java object is treated as actual xml content.
> > Here's an example (assumes you have the util logicsheet declared in
> > the
> > "util" namespace):
> >
> > ...
> > <xsp:logic>
> >       MyXmlSource xmlSource = new MyXmlSource();
> >
> >       ... init xmlSource and do whatever you need to prepare content
> > ...
> >
> >       String xmlContent = xmlSource.getContent();
> >
> >
> >
> <util:include-expr><util:expr><xsp:expr>xmlContent</xsp:expr></util:expr></u
> > til:include-expr>
> > </xsp:logic>
> > ...
> >
> > If I remember right, the util logicsheet works by using the XMLizable
> > object
> > underneath, so it's basically a cleaner interface for the approach
> > I had
> > mentioned previously.  =)  Anyway, hope that helps!
> >
> > Harry
> >
> >
> > -----Original Message-----
> > From: ptv@alberta.com [mailto:ptv@alberta.com]
> > Sent: Wednesday, May 15, 2002 11:12 AM
> > To: HLai@coremetrics.com
> > Subject: Question about best approach to transform Java Object to XML
> > represention for use in Cocoon
> >
> >
> >
> >
> > Hi Harry,
> >
> > I am working with Cocoon2 and I am trying to sort out  the exact same
> > problem you raised a while back in one of the posts you sent to the
> > mailing
> > list and so I am hoping you can give me some advice. I would like to
> > output an xml representation of a Java object so that it can be sent
> > to a stylesheet for tranformation into HTML. I include your post to
> > the
> > mailing list  at the end of this email for your reference.
> >
> > Did you have any success with this problem? If you have can you give
> > your insight on the best practice approach you would recommend to output
> > xml from Java object?
> >
> > Is XMLizable the best way to go?  I also read in the mailing archives
> > about the Castor Transformer.
> >
> > I originally thought the best way to do this would be to output the
> > xml
> > in a logicsheet. And so the idea is to pass a Java object into a
> logicsheet
> > and then you would traverse the java object and output XML using templates
> > in the logicsheet. It seemed like a good idea because outputting XML
> > tags in a logicsheet is very easy. But what I found is that  logicsheets
> > don't handle Java code too well and it gets messy when you start having
> > even a little bit of java code in a logic sheet.
> >
> > Anyways, I would be very interested in gaining some insight from your
> > experience.
> >
> > Also if you recommend the XMLizable approach, could you send me a sample
> > showing how it works?  I have looked in vain but cannot see any examples
> > or documentation to get me started on this route.
> >
> >
> > Thanks,
> > Phong
> >
> > PS
> > Below is your original post
> >
> > Hi all,
> >
> > Murphy's Law, but I think I've solved our problem a minute after sending
> > this out.  Anyway, for those interested, I found that there's an XMLizable
> > interface, and if your object returns XMLizable (instead of a String),
> > xsp:expr calls your XMLizable's toSAX() method instead of outputting
> > your
> > return value as a text node.  No changes to cocoon needed.  =)
> >
> > Feeling kinda slow on the uptake,
> > Harry
> >
> >
> > -----Original Message-----
> > From: Lai, Harry [mailto:HLai@coremetrics.com]
> > Sent: Thursday, December 06, 2001 5:58 PM
> > To: 'CocoonUsers'
> > Subject: Inserting dynamic XML at request time
> >
> >
> > Hi all,
> >
> > I have a question that's related to the "Inserting / Comining XML data"
> > thread, but which I believe is slightly different.  Here's the situation.
> >
> > SCENARIO
> > -----------------
> > We are using Cocoon 2, and we would like to use an XSP doc as a "shell"
> > that
> > aggregates XML data from one or more Java objects (the objects would
> > actually output XML strings).  These Java objects (possibly EJB's)
> > would
> > need access to the Request object in order to do their processing.
> >  So,
> > here's what we originally tried (most of you will probably see the
> > flaw
> > pretty quickly).
> >
> > PROBLEM
> > ----------------
> > We have a Java object like this (this sample is a simplified version
> > of what
> > we're actually planning to do, but it illustrates the problem we're
> > running
> > into):
> >
> >     public class Foo {
> >         public getXML(Request req) {
> >             return "<foo>This is a foo tag.</foo>";
> >         }
> >     }
> >
> > And we have this XSP doc:
> >
> >     <xsp:page language="java" xmlns:xsp=" http://apache.org/xsp
> > <http://apache.org/xsp> ">
> > <xsp:structure>
> > <xsp:include>com.coremetrics.test.Foo</xsp:include>
> > </xsp:structure>
> > <xsp:logic>
> > Foo f = new Foo();
> >         </xsp:logic>
> > <page>
> > <xsp:expr>f.getXML(request)</xsp:expr>
> > </page>
> > </xsp:page>
> > Unfortunately, <xsp:expr> renders the return value as a text node,
> > so
> > we got
> > the text content "<foo%gt;..." as the content of our page node in
> > the
> > resulting xml.  SO...
> >
> >
> > ___________________________________________________________
> > FREE voicemail, email and fax, all in one place.  Sign Up Now!
> > http://www.alberta.com
> >
>
> ___________________________________________________________
> FREE voicemail, email and fax, all in one place.  Sign Up Now!
> http://www.alberta.com
>
>   ------------------------------------------------------------------------
> ---------------------------------------------------------------------
> Please check that your question has not already been answered in the
> FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>
>
> To unsubscribe, e-mail: <co...@xml.apache.org>
> For additional commands, e-mail: <co...@xml.apache.org>


---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

To unsubscribe, e-mail: <co...@xml.apache.org>
For additional commands, e-mail: <co...@xml.apache.org>