
// XMLReader.java
// Geir Magnusson Jr, March 2004
// Modified by Andrew Davison, ad@coe.psu.ac.th, July 2023

/* Display XML data (read from test.xml) using a template
   (xml.vm) and recursive macro.
   XML is supported using the JDOM library.
   (http://www.jdom.org/).

  Usage:
    > compile XMLReader.java
    > run XMLReader
*/

import java.io.*;
import org.jdom.Document;
import org.jdom.input.SAXBuilder;

import org.apache.velocity.*;
import org.apache.velocity.app.*;


public class XMLReader
{
  private static final String TEMPLATE_FNM = "xml.vm";
  private static final String XML_FNM = "test.xml";


  public static void main(String[] args) throws Exception
  {
    Velocity.init();
    System.out.println("Loading template: " + TEMPLATE_FNM);
    Template template = Velocity.getTemplate(TEMPLATE_FNM);
     
    System.out.println("Loading XML file: " + XML_FNM);
    SAXBuilder builder = new SAXBuilder();
    Document root = builder.build(XML_FNM);

    VelocityContext vc = new VelocityContext();
    vc.put("root", root);

    StringWriter sw = new StringWriter();
    template.merge(vc, sw);
    System.out.println(sw);
  }  // end of main()

}  // end of XMLReader class


