You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by peter1111 <jo...@web.de> on 2007/03/17 20:59:57 UTC

Velocity Servlet and Sax Events

Hello!
 
I want to write a Servlet basing on the velocity-template-engine.
Everything works fine, I have only difficultiies reading a XML file with SAX
and getting the sax-events into the velocity-context.

Could anyone give me a hint how to tell SAX to work with Velocity ?
 
My idea would be the following, but it does not work...

//*************************************

public class TestServlet extends VelocityServlet implements ContentHandler {

String testString;

 protected Properties loadConfiguration(ServletConfig config )
        throws IOException, FileNotFoundException    {
   //......
 }


  /*** SAX - Methods ***********************/    
   
    
    public void startDocument() throws SAXException {    	
       // ......
    }      
       
      
    public void startElement( String namespaceURI, String localName, String
qName, Attributes atts) throws    SAXException {    	
      
    // element "page" in my xml-file
    	if ( qName.equalsIgnoreCase( "page" )) {
    		
        // just a very simple event..
    		testString = "testString";		
    		
    	}

/*

... other possible Sax-Events

*/

 public Template handleRequest( HttpServletRequest request,
	HttpServletResponse response, Context ctx )    {        


       try {   		    		
    		
    		String parserClass ="org.apache....."
    		TestServlet testServlet = new TestServlet();    	    		
    		XMLReader reader = XMLReaderFactory.createXMLReader(parserClass);    		
    		reader.setContentHandler(testServlet);
    		reader.parse( "xml/test.xml");
       } catch 
          // ..Exceptions ...


// -> THIS does not work, in the velocity-template I get for 'testString'
only $testString ...
ctx.put( "testString", testString);



//... end of velocity-servlet


/******************************/

I have no problems getting variables etc. into the velocity-context.
My problem here I think are the SAX-Events...


Thanks in advance !

Peter


-- 
View this message in context: http://www.nabble.com/Velocity-Servlet-and-Sax-Events-tf3420318.html#a9532823
Sent from the Velocity - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org


Re: Velocity Servlet and Sax Events

Posted by Nathan Bubna <nb...@gmail.com>.
On 3/17/07, peter1111 <jo...@web.de> wrote:
>
> Hello!
>
> I want to write a Servlet basing on the velocity-template-engine.
> Everything works fine, I have only difficultiies reading a XML file with SAX
> and getting the sax-events into the velocity-context.
>
> Could anyone give me a hint how to tell SAX to work with Velocity ?

depends on how you want them to work together....

> My idea would be the following, but it does not work...
>
> //*************************************
>
> public class TestServlet extends VelocityServlet implements ContentHandler {
>
> String testString;
>
>  protected Properties loadConfiguration(ServletConfig config )
>         throws IOException, FileNotFoundException    {
>    //......
>  }
>
>
>   /*** SAX - Methods ***********************/
>
>
>     public void startDocument() throws SAXException {
>        // ......
>     }
>
>
>     public void startElement( String namespaceURI, String localName, String
> qName, Attributes atts) throws    SAXException {
>
>     // element "page" in my xml-file
>         if ( qName.equalsIgnoreCase( "page" )) {
>
>         // just a very simple event..
>                 testString = "testString";
>
>         }
>
> /*
>
> ... other possible Sax-Events
>
> */
>
>  public Template handleRequest( HttpServletRequest request,
>         HttpServletResponse response, Context ctx )    {
>
>
>        try {
>
>                 String parserClass ="org.apache....."
>                 TestServlet testServlet = new TestServlet();
>                 XMLReader reader = XMLReaderFactory.createXMLReader(parserClass);
>                 reader.setContentHandler(testServlet);
>                 reader.parse( "xml/test.xml");
>        } catch
>           // ..Exceptions ...
>
>
> // -> THIS does not work, in the velocity-template I get for 'testString'
> only $testString ...
> ctx.put( "testString", testString);

make sure this call to ctx.put() happens before the template is merged
with the context.  also, you should make sure that testString is not
null when you try to put it into the context.

>
> //... end of velocity-servlet
>
>
> /******************************/
>
> I have no problems getting variables etc. into the velocity-context.
> My problem here I think are the SAX-Events...
>
>
> Thanks in advance !
>
> Peter
>
>
> --
> View this message in context: http://www.nabble.com/Velocity-Servlet-and-Sax-Events-tf3420318.html#a9532823
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org