You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by "Peter C. Verhage" <pe...@zeelandnet.nl> on 2001/02/26 18:24:21 UTC

Return XML from class

I'm creating some classes that have to return XML when a certain method is
called. I looked at the FAQ how to do this, and there are two possibilities:

1:
The easiest way is to simply replace <xsp:expr> with <util:include-expr>
(making sure to declare the util logicsheet namespace in your <xsp:page>
tag - instructions for this can be found in the XSP documentation). You must
either use the form <util:include-expr expr="constant string"/> for literals
or <util:include-expr><util:expr>mymethod()</util:expr> </util:include-expr>
for methods and expressions which evaluate to strings. However this is not
necessarily the most efficient way, because it invokes the installed XML
parser to parse the string into an in-memory representation used throughout
Cocoon 1.x called a DOM (Document Object Model) .

2:
It is thus better to generate DOM objects directly. This is outside the
scope of this documentation (except insofar as it documents the
specification of <xsp:expr>) - see the DOM link above, and also the question
on "good books". You can also generate org.apache.cocoon.xml.XMLFragments,
and return both DOM and SAX, thus making your code (hopefully) more
forward-compatible with Cocoon 2, which will be SAX-based. (Actually using
SAX is not strictly necessary, since DOM can always be converted to SAX and
vice-versa, but you will pay a performance penalty in Cocoon 2 by using
DOM.) Note that with either of these, you should still use <xsp:expr>, not
<util:include-expr>.

As you might understand, I prefer the last method because it's faster... I
looked at how to create a XMLFragment, unfortunately I don't quite
understand how to do this in my class. I just want to return an object that
can be interpreted by Cocoon as XML. And if necessary I want to create a
class that can be returned but I don't quite understand how to do this
within my class... At the bottom of the example page there is also this
note:

"Of course, it is very wasteful to write two methods to do exactly the same
thing! That is why we suggest people implement only toSAX directly, and then
form a DOM tree from the SAX events in the toDOM method. An XMLFragmentBase
class is in development to simplify this."

Is this XMLFragmentBase already available?

Regards,

Peter