You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Marko Niinimaki <ma...@mail.cern.ch> on 2002/02/11 14:02:51 UTC
How do I generate XML in cocoon 2, as in cocoon 1.x?
I have a little java program that returns a string that contains XML.
In order to create a html output of it, I've used the following stuff:
<xsp:logic><![CDATA[
String s = "<foo>hello</foo>";
//of course, in real life, I call an external program that
//generates s, like s = DataAccess.getLoginName..
]]>
</xsp:logic>
<xsp:expr>
this.xspParser.parse(
new InputSource(
new StringReader(s)
)
).getDocumentElement()
</xsp:expr>
How do I do the same stuff with cocoon 2?
Yours,
Marko
---------------------------------------------------------------------
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>
RE: How do I generate XML in cocoon 2, as in cocoon 1.x?
Posted by Vadim Gritsenko <va...@verizon.net>.
> From: Marko Niinimaki [mailto:man@mail.cern.ch]
>
> Hello,
>
> thanks for the answer (please see below)
>
> On Mon, 11 Feb 2002, Vadim Gritsenko wrote:
>
> > In Cocoon 2.X, no xspParser is in XSPGenerator. IIRC, this was not
> > heavily advertised in Cocoon 1.X also. So if you need a parser it's
now
> > your responsibility now to lookup it from the component manager and
> > release it (don't forget to release, best place is finally
statement).
> >
> > Then, I see couple of ways:
> >
> > 1. SAX: Use EmbeddedXMLPipe between this.contentHandler and parser
to
> > filter out start/end document.
> > 2. DOM: Parse string as DOM and then stream the DOM to the
> > this.contentHandler using DOMStreamer.
>
> Ok, I cooked up (on the basis of the view-source example) the
following,
> my sitemap states that this is a serverpage and simple-page2html.xsl
> is used to transform it. But the output to the browser is just
> the match of "page". If I try to match "foo", nothing comes out.
> Is there an apparent mistake in the code? Or a tool by which I can
> see the DOM tree of the xsp?
>
>
> <?xml version="1.0"?>
>
> <xsp:page language="java"
> xmlns:xsp="http://apache.org/xsp"
> >
> <xsp:structure>
> <xsp:include>java.net.*</xsp:include>
> <xsp:include>org.w3c.dom.*</xsp:include>
> <xsp:include>org.apache.cocoon.xml.dom.DOMStreamer</xsp:include>
> <xsp:include>org.apache.cocoon.xml.dom.DOMBuilder</xsp:include>
>
<xsp:include>org.apache.cocoon.components.parser.Parser</xsp:include>
>
<xsp:include>org.apache.cocoon.components.url.URLFactory</xsp:include>
> </xsp:structure>
> <page>
> <xsp:logic><![CDATA[
> String s = "<foo>yo</foo>";
> Parser newParser = null;
> try {
> newParser = (Parser) this.manager.lookup(Parser.ROLE);
> Document document = newParser.newDocument();
> DOMBuilder builder = new DOMBuilder(newParser);
> newParser.setContentHandler(builder);
> InputSource is = new InputSource(new StringReader(s));
> newParser.parse(is);
> DOMStreamer ds = new DOMStreamer (this.contentHandler);
> ds.stream (newParser.parseDocument(is));
Why you parse it twice? Can't you just do something like:
parser.setContentHandler(new EmbeddedXMLPipe(this.contentHandler));
parser.parse(new InputSource(new StringReader(s)))
?
Also, if you need to debug this, you can use:
1. XML serializer right after severpages generator to view XML as
produced by page, or LoggingTransformer after serverpages generator
2. LoggingContentHandler
Vadim
> } catch (Exception e) {
> System.out.println(e);
> } finally {
> this.manager.release((Component) newParser);
> }
> ]]>
> </xsp:logic>
> </page>
> </xsp:page>
---------------------------------------------------------------------
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>
RE: How do I generate XML in cocoon 2, as in cocoon 1.x?
Posted by Marko Niinimaki <ma...@mail.cern.ch>.
Hello,
thanks for the answer (please see below)
On Mon, 11 Feb 2002, Vadim Gritsenko wrote:
> In Cocoon 2.X, no xspParser is in XSPGenerator. IIRC, this was not
> heavily advertised in Cocoon 1.X also. So if you need a parser it's now
> your responsibility now to lookup it from the component manager and
> release it (don't forget to release, best place is finally statement).
>
> Then, I see couple of ways:
>
> 1. SAX: Use EmbeddedXMLPipe between this.contentHandler and parser to
> filter out start/end document.
> 2. DOM: Parse string as DOM and then stream the DOM to the
> this.contentHandler using DOMStreamer.
Ok, I cooked up (on the basis of the view-source example) the following,
my sitemap states that this is a serverpage and simple-page2html.xsl
is used to transform it. But the output to the browser is just
the match of "page". If I try to match "foo", nothing comes out.
Is there an apparent mistake in the code? Or a tool by which I can
see the DOM tree of the xsp?
<?xml version="1.0"?>
<xsp:page language="java"
xmlns:xsp="http://apache.org/xsp"
>
<xsp:structure>
<xsp:include>java.net.*</xsp:include>
<xsp:include>org.w3c.dom.*</xsp:include>
<xsp:include>org.apache.cocoon.xml.dom.DOMStreamer</xsp:include>
<xsp:include>org.apache.cocoon.xml.dom.DOMBuilder</xsp:include>
<xsp:include>org.apache.cocoon.components.parser.Parser</xsp:include>
<xsp:include>org.apache.cocoon.components.url.URLFactory</xsp:include>
</xsp:structure>
<page>
<xsp:logic><![CDATA[
String s = "<foo>yo</foo>";
Parser newParser = null;
try {
newParser = (Parser) this.manager.lookup(Parser.ROLE);
Document document = newParser.newDocument();
DOMBuilder builder = new DOMBuilder(newParser);
newParser.setContentHandler(builder);
InputSource is = new InputSource(new StringReader(s));
newParser.parse(is);
DOMStreamer ds = new DOMStreamer (this.contentHandler);
ds.stream (newParser.parseDocument(is));
} catch (Exception e) {
System.out.println(e);
} finally {
this.manager.release((Component) newParser);
}
]]>
</xsp:logic>
</page>
</xsp:page>
---------------------------------------------------------------------
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>
RE: How do I generate XML in cocoon 2, as in cocoon 1.x?
Posted by Vadim Gritsenko <va...@verizon.net>.
> From: Marko Niinimaki [mailto:man@mail.cern.ch]
>
> I have a little java program that returns a string that contains XML.
> In order to create a html output of it, I've used the following stuff:
>
> <xsp:logic><![CDATA[
> String s = "<foo>hello</foo>";
> //of course, in real life, I call an external program that
> //generates s, like s = DataAccess.getLoginName..
> ]]>
> </xsp:logic>
>
>
> <xsp:expr>
> this.xspParser.parse(
> new InputSource(
> new StringReader(s)
> )
> ).getDocumentElement()
> </xsp:expr>
>
> How do I do the same stuff with cocoon 2?
In Cocoon 2.X, no xspParser is in XSPGenerator. IIRC, this was not
heavily advertised in Cocoon 1.X also. So if you need a parser it's now
your responsibility now to lookup it from the component manager and
release it (don't forget to release, best place is finally statement).
Then, I see couple of ways:
1. SAX: Use EmbeddedXMLPipe between this.contentHandler and parser to
filter out start/end document.
2. DOM: Parse string as DOM and then stream the DOM to the
this.contentHandler using DOMStreamer.
Vadim
>
> Yours,
> Marko
>
---------------------------------------------------------------------
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>