You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Chris Hitzke <ch...@adobe.com> on 2004/03/04 22:37:24 UTC

JDOM and Velocity to transform from XML to HTML using Velocity Template



I am using Anakia along with JDOM and Velocity to transform my XML document 
into
HTML file. Below, is the code I wrote to make the transformation happen. 
But, it's not utilizing
the AnakiaTask class. How do I utilize the AnikiaTask class to convert XML 
to HTML using Velocity
template? I see how the it's done in the build.xml file (Anikia example). 
But, how do you do it
programatically? Please, respond. Thanks....


assumption:
==========

xml file
===========
xml tags and info

.vm file
======
contains velocity code and html tags

html file
======
result of transforming xml file to .vm file

code for conversion
===================

Writer writer = null;

try
{
/*
  *  and now call init
  */

Velocity.init();


/*
  * build a Document from our xml
  */

SAXBuilder builder;
Document root = null;

try
{
builder = new SAXBuilder(  "org.apache.xerces.parsers.SAXParser" );
root = builder.build(resultsFileName);
}
catch( Exception ee)
{
System.out.println("Exception building Document : " + ee);
return;
}

/*
  * now, make a Context object and populate it.
  */

VelocityContext context = new VelocityContext();
context.put("root", root);

/*
  *  make a writer, and merge the template 'against' the context
  */

Template template = Velocity.getTemplate(velocityTemplateFileName);

writer = new BufferedWriter(new OutputStreamWriter(System.out));
template.merge( context , writer);
}
catch( Exception e )
{
    System.out.println("Exception : " + e);
}
finally
{
if ( writer != null)
{
try
{
writer.flush();
writer.close();
}
catch( Exception ee )
{
System.out.println("Exception : " + ee );
}
}
}


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


Re: JDOM and Velocity to transform from XML to HTML using Velocity Template

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
Use DVSL (and don't crosspost any continuation of this thread - choose 
user or dev :)

geir

On Mar 4, 2004, at 4:37 PM, Chris Hitzke wrote:

>
>
>
> I am using Anakia along with JDOM and Velocity to transform my XML 
> document into
> HTML file. Below, is the code I wrote to make the transformation 
> happen. But, it's not utilizing
> the AnakiaTask class. How do I utilize the AnikiaTask class to convert 
> XML to HTML using Velocity
> template? I see how the it's done in the build.xml file (Anikia 
> example). But, how do you do it
> programatically? Please, respond. Thanks....
>
>
> assumption:
> ==========
>
> xml file
> ===========
> xml tags and info
>
> .vm file
> ======
> contains velocity code and html tags
>
> html file
> ======
> result of transforming xml file to .vm file
>
> code for conversion
> ===================
>
> Writer writer = null;
>
> try
> {
> /*
>  *  and now call init
>  */
>
> Velocity.init();
>
>
> /*
>  * build a Document from our xml
>  */
>
> SAXBuilder builder;
> Document root = null;
>
> try
> {
> builder = new SAXBuilder(  "org.apache.xerces.parsers.SAXParser" );
> root = builder.build(resultsFileName);
> }
> catch( Exception ee)
> {
> System.out.println("Exception building Document : " + ee);
> return;
> }
>
> /*
>  * now, make a Context object and populate it.
>  */
>
> VelocityContext context = new VelocityContext();
> context.put("root", root);
>
> /*
>  *  make a writer, and merge the template 'against' the context
>  */
>
> Template template = Velocity.getTemplate(velocityTemplateFileName);
>
> writer = new BufferedWriter(new OutputStreamWriter(System.out));
> template.merge( context , writer);
> }
> catch( Exception e )
> {
>    System.out.println("Exception : " + e);
> }
> finally
> {
> if ( writer != null)
> {
> try
> {
> writer.flush();
> writer.close();
> }
> catch( Exception ee )
> {
> System.out.println("Exception : " + ee );
> }
> }
> }
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


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