You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Anton Jansen <gr...@fmf.nl> on 2002/01/02 12:05:27 UTC

XML parsing in XSP

Hi all,

I want to be able to parse a java string to an xml representation in a XSP.
Looking at the source code for the StreamGenerator (which takes a http-post
stream) i have written the following code to parse a xml string in xsp:

        Parser parser = null;
        try {
          org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(
            new StringReader(xmldata)
          );
          this.getLogger().debug("Looking up parser");
          parser = (Parser)this.manager.lookup(Parser.ROLE);
          parser.setContentHandler(this.contentHandler);
          parser.setLexicalHandler(this.lexicalHandler);
          parser.parse(inputSource);
        } catch (Exception ex) {
          ex.printStackTrace();
          this.getLogger().debug(ex.getMessage());
        } finally {
          if (parser != null) {
            this.getLogger().debug("Releasing parser");
            this.manager.release((Component)parser);
          }
        }

Where xmldata is a string containing the xml data in plain ASCII. Okay, this
works, i can serialize the data generated. Problem is that the moment i add
a transform after the generate of this XSP page in the sitemap I get a null
pointer exception in the XSP. The xsp exception seems to
be raised by the last instruction in the generated XSP java class:

 this.contentHandler.endDocument();

Gives the following stacktrace:
Original exception : java.lang.RuntimeException:
java.lang.NullPointerException
	at
org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:3174)
	at
org.apache.xalan.transformer.TransformerHandlerImpl.endDocument(TransformerH
andlerImpl.java:433)
	at
org.apache.cocoon.xml.AbstractXMLPipe.endDocument(AbstractXMLPipe.java:48)
	at
org.apache.cocoon.www.file_.C_.Programming.jakarta_tomcat_4_0_1.webapps.coco
on.athena.course_xsp.generate(course_xsp.java:153)
	at
org.apache.cocoon.generation.ServerPagesGenerator.generate(ServerPagesGenera
tor.java:224)
	at
org.apache.cocoon.components.pipeline.CachingEventPipeline.process(CachingEv
entPipeline.java:210)
	at
org.apache.cocoon.components.pipeline.CachingStreamPipeline.process(CachingS
treamPipeline.java:358)
	at
org.apache.cocoon.www.file_.C_.Programming.jakarta_tomcat_4_0_1.webapps.coco
on.athena.sitemap_xmap.wildcardMatchN400028(sitemap_xmap.java:471)
	at
org.apache.cocoon.www.file_.C_.Programming.jakarta_tomcat_4_0_1.webapps.coco
on.athena.sitemap_xmap.process(sitemap_xmap.java:342)
	at
org.apache.cocoon.www.file_.C_.Programming.jakarta_tomcat_4_0_1.webapps.coco
on.athena.sitemap_xmap.process(sitemap_xmap.java:294)
	at org.apache.cocoon.sitemap.Handler.process(Handler.java:171)
	at org.apache.cocoon.sitemap.Manager.invoke(Manager.java:115)
	at
org.apache.cocoon.www.sitemap_xmap.wildcardMatchN400244(sitemap_xmap.java:34
49)
	at org.apache.cocoon.www.sitemap_xmap.process(sitemap_xmap.java:2707)
	at org.apache.cocoon.www.sitemap_xmap.process(sitemap_xmap.java:2636)
	at org.apache.cocoon.sitemap.Handler.process(Handler.java:171)
	at org.apache.cocoon.sitemap.Manager.invoke(Manager.java:115)
	at
org.apache.cocoon.sitemap.SitemapManager.process(SitemapManager.java:144)
	at org.apache.cocoon.Cocoon.process(Cocoon.java:608)
	at org.apache.cocoon.servlet.CocoonServlet.service(CocoonServlet.java:793)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
FilterChain.java:247)
	at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
ain.java:193)
	at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
va:243)
	at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
66)
	at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
	at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
va:201)
	at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
66)
	at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
	at
org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)
	at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164
)
	at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
66)
	at
org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.
java:170)
	at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
64)
	at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170
)
	at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
64)
	at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)
	at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
64)
	at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
	at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java
:163)
	at
org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
66)
	at
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
	at
org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:
1011)
	at
org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106
)
	at java.lang.Thread.run(Thread.java:484)

With trial and error i have found that the parser.parse(inputSource) command
is the source of this problem, but it also fullfills my wish to be able to
parse a string at runtime.

Has anyone an idea what I'm doing wrong and maybe a solution?
Thanks,
Anton Jansen





---------------------------------------------------------------------
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: XML parsing in XSP

Posted by David Rosenstrauch <da...@dti.net>.
Another option you can use to generate xml output from a string in an XSP page:

Use <util:include-expr>.  It will parse the text that you pass in to it, and generate SAX calls from it (as opposed to writing the text straight to the output - after escaping all the control chars - like it's doing now).

1) Include the namespace xmlns:util="http://apache.org/xsp/util/2.0" in your XSP or the <util:include-expr> won't get called properly.

2) the call you need to make this work:
<util:include-expr><util:expr><xsp:expr>myString</xsp:expr></util:expr></util:include-expr>

<xsp:expr>myString</xsp:expr> takes the value of String  myString, which is then passed in to <util:include-expr>.


DR



At 12:05 PM 1/2/02 +0100, you wrote:
>Hi all,
>
>I want to be able to parse a java string to an xml representation in a XSP.
>Looking at the source code for the StreamGenerator (which takes a http-post
>stream) i have written the following code to parse a xml string in xsp:
>
>         Parser parser = null;
>         try {
>           org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(
>             new StringReader(xmldata)
>           );
>           this.getLogger().debug("Looking up parser");
>           parser = (Parser)this.manager.lookup(Parser.ROLE);
>           parser.setContentHandler(this.contentHandler);
>           parser.setLexicalHandler(this.lexicalHandler);
>           parser.parse(inputSource);
>         } catch (Exception ex) {
>           ex.printStackTrace();
>           this.getLogger().debug(ex.getMessage());
>         } finally {
>           if (parser != null) {
>             this.getLogger().debug("Releasing parser");
>             this.manager.release((Component)parser);
>           }
>         }
>
>Where xmldata is a string containing the xml data in plain ASCII. Okay, this
>works, i can serialize the data generated. Problem is that the moment i add
>a transform after the generate of this XSP page in the sitemap I get a null
>pointer exception in the XSP. The xsp exception seems to
>be raised by the last instruction in the generated XSP java class:
>
>  this.contentHandler.endDocument();
>
>Gives the following stacktrace:
>Original exception : java.lang.RuntimeException:
>java.lang.NullPointerException
>         at
>org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:3174)
>         at
>org.apache.xalan.transformer.TransformerHandlerImpl.endDocument(TransformerH
>andlerImpl.java:433)
>         at
>org.apache.cocoon.xml.AbstractXMLPipe.endDocument(AbstractXMLPipe.java:48)
>         at
>org.apache.cocoon.www.file_.C_.Programming.jakarta_tomcat_4_0_1.webapps.coco
>on.athena.course_xsp.generate(course_xsp.java:153)
>         at
>org.apache.cocoon.generation.ServerPagesGenerator.generate(ServerPagesGenera
>tor.java:224)
>         at
>org.apache.cocoon.components.pipeline.CachingEventPipeline.process(CachingEv
>entPipeline.java:210)
>         at
>org.apache.cocoon.components.pipeline.CachingStreamPipeline.process(CachingS
>treamPipeline.java:358)
>         at
>org.apache.cocoon.www.file_.C_.Programming.jakarta_tomcat_4_0_1.webapps.coco
>on.athena.sitemap_xmap.wildcardMatchN400028(sitemap_xmap.java:471)
>         at
>org.apache.cocoon.www.file_.C_.Programming.jakarta_tomcat_4_0_1.webapps.coco
>on.athena.sitemap_xmap.process(sitemap_xmap.java:342)
>         at
>org.apache.cocoon.www.file_.C_.Programming.jakarta_tomcat_4_0_1.webapps.coco
>on.athena.sitemap_xmap.process(sitemap_xmap.java:294)
>         at org.apache.cocoon.sitemap.Handler.process(Handler.java:171)
>         at org.apache.cocoon.sitemap.Manager.invoke(Manager.java:115)
>         at
>org.apache.cocoon.www.sitemap_xmap.wildcardMatchN400244(sitemap_xmap.java:34
>49)
>         at org.apache.cocoon.www.sitemap_xmap.process(sitemap_xmap.java:2707)
>         at org.apache.cocoon.www.sitemap_xmap.process(sitemap_xmap.java:2636)
>         at org.apache.cocoon.sitemap.Handler.process(Handler.java:171)
>         at org.apache.cocoon.sitemap.Manager.invoke(Manager.java:115)
>         at
>org.apache.cocoon.sitemap.SitemapManager.process(SitemapManager.java:144)
>         at org.apache.cocoon.Cocoon.process(Cocoon.java:608)
>         at org.apache.cocoon.servlet.CocoonServlet.service(CocoonServlet.java:793)
>         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
>         at
>org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Application
>FilterChain.java:247)
>         at
>org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterCh
>ain.java:193)
>         at
>org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.ja
>va:243)
>         at
>org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
>66)
>         at
>org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
>         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
>         at
>org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.ja
>va:201)
>         at
>org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
>66)
>         at
>org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
>         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
>         at
>org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)
>         at
>org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164
>)
>         at
>org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
>66)
>         at
>org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.
>java:170)
>         at
>org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
>64)
>         at
>org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170
>)
>         at
>org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
>64)
>         at
>org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:462)
>         at
>org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
>64)
>         at
>org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
>         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
>         at
>org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java
>:163)
>         at
>org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:5
>66)
>         at
>org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)
>         at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)
>         at
>org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:
>1011)
>         at
>org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106
>)
>         at java.lang.Thread.run(Thread.java:484)
>
>With trial and error i have found that the parser.parse(inputSource) command
>is the source of this problem, but it also fullfills my wish to be able to
>parse a string at runtime.
>
>Has anyone an idea what I'm doing wrong and maybe a solution?
>Thanks,
>Anton Jansen
>
>
>
>
>
>---------------------------------------------------------------------
>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>