Header Ads

  • Breaking Now

    Velocity With XML

    In this post, a code snippet describing a basic integration of XML with Velocity framework. Please ensure all runtime jar files associated with Velocity and Xerces XML parser are in your build path.

    The following files are part of this code snippet;

    1.XMLVelocityExample.java
    2.xml.vm
    3.book.xml

    Here goes the Java code i.e. XMLVelocityExample.java:



    import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.jdom.Document; import org.jdom.input.SAXBuilder; import java.io.BufferedWriter; import java.io.OutputStreamWriter; import java.io.Writer; public class XMLVelocityExample { public XMLVelocityExample(String templateFile) { Writer writer = null; try { Velocity.init(); SAXBuilder builder; Document root = null; try { builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser"); root = builder.build("book.xml"); } catch (Exception exception) { exception.printStackTrace(); } VelocityContext context = new VelocityContext(); context.put("root", root); Template template = Velocity.getTemplate(templateFile); writer = new BufferedWriter(new OutputStreamWriter(System.out)); template.merge(context, writer); } catch (Exception ex) { ex.printStackTrace(); } finally { if (writer != null) { try { writer.flush(); writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } public static void main(String[] args) { XMLVelocityExample test = new XMLVelocityExample("xml.vm"); } }


    xml.vm looks like:

    #macro ( recursive $xml $indent ) #if( $xml.getChildren().size() > 0 ) $indent <$xml.getName()> #foreach ($child in $xml.getChildren() ) #recursive( $child "$indent " ) #end $indent </$xml.getName()> #else $indent <$xml.getName()> $indent $xml.getTextTrim() $indent </$xml.getName()> #end #end #set($i = " ") Here goes document tree with a recursive Velocity macro : #recursive( $root.getRootElement() $i ) Now accessing data directly : email : $root.getRootElement().getChild("properties").getChild("author").getChild("email").getText() last name : $root.getRootElement().getChild("properties").getChild("author").getChild("name").getChild("fullname").getText()


    book.xml file looks like:

    <?xml version="1.0" encoding="UTF-8"?> <document> <properties> <author> <email>sunnyg@sunnyg.com</email> <name> <lastname> Gavaskar </lastname> <first> Sunil </first> <fullname>Sunil Gavaskar</fullname> </name> </author> <title> Sunny Days </title> </properties> <body> Sunny within a body. </body> </document>


    This is how output looks like:
    Here goes document tree with a recursive Velocity macro : <document> <properties> <author> <email> sunnyg@sunnyg.com </email> <name> <lastname> Gavaskar </lastname> <first> Sunil </first> <fullname> Sunil Gavaskar </fullname> </name> </author> <title> Sunny Days </title> </properties> <body> Sunny within a body. </body> </document> Now accessing data directly : email : sunnyg@sunnyg.com last name : Sunil Gavaskar

    No comments

    Post Top Ad

    Post Bottom Ad