Return to Snippet

Revision: 15074
at June 23, 2009 11:39 by gripnrip


Updated Code
import groovy.text.XmlTemplateEngine
import java.util.Properties
import java.io.File
import java.io.FileWriter

//Takes a java props file, an XML template file and creates the given output file
void createFile(propertiesFile, templateFileName, outputFileName) {
	// read properties file given
	def props = new Properties()
	props.load(new FileInputStream(new File(propertiesFile)))
	
	// map to the bindings
	def bindings = [:]
	props.propertyNames().each{prop->
		bindings[prop]=props.getProperty(prop)
	}

	// create the template and make the output file
	def engine = new XmlTemplateEngine()
	def templateFile = new File(templateFileName)
	def output = engine.createTemplate(templateFile).make(bindings)
	
	def outputFile = new File(outputFileName)
	def parentFile = outputFile.getParentFile()
	if (parentFile != null)	parentFile.mkdirs()
	def fileWriter = new FileWriter(outputFile)
	fileWriter.write(output.toString())
	fileWriter.close()
}

Revision: 15073
at June 23, 2009 11:39 by gripnrip


Initial Code
import groovy.text.XmlTemplateEngine
import java.util.Properties
import java.io.File
import java.io.FileWriter

//Takes a java props file, an XML template file and creates the given output file
void createFile(propertiesFile, templateFileName, outputFileName) {
	// read properties file given
	def props = new Properties()
	props.load(new FileInputStream(new File(propertiesFile)))
	
	// map to the bindings
	def bindings = [:]
	props.propertyNames().each{prop->
		//bindings.put(prop,props.getProperty(prop))
		bindings[prop]=props.getProperty(prop)
	}

	// create the template and make the output file
	def engine = new XmlTemplateEngine()
	def templateFile = new File(templateFileName)
	def output = engine.createTemplate(templateFile).make(bindings)
	
	def outputFile = new File(outputFileName)
	def parentFile = outputFile.getParentFile()
	if (parentFile != null)	parentFile.mkdirs()
	def fileWriter = new FileWriter(outputFile)
	fileWriter.write(output.toString())
	fileWriter.close()
}

Initial URL

                                

Initial Description
Groovy script that creates an XML file using an XML template containing ${params} and a standard Java Properties file

Initial Title
Create XML from Template and Properties

Initial Tags
java, xml, groovy

Initial Language
Groovy