File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Generating XML from template file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Generating XML from template file." Watch "Generating XML from template file." New topic
Author

Generating XML from template file.

Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
I have to generate XML documents dynamically from a template file and a flat file containting the values

For example, say I have a Template like this

And the flat file will have something like

I need to merge these 2 files and get the following output

What are all the possible ways to do this?
Are there any tools available to do this?


Mani
Quaerendo Invenietis
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Here's something to start from:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Thanks for the reply Lasse.
I should have explained it bit clearly in my initial post. Actually I was looking for various possible ways to acheive this. Using Java is one way, but is there some other way?
I need this tool to be as generic as possible. It should be possible for me to modify the variable file and / or the template file with out changing the code.
I could very well use your logic and create this utility, but I would like to check if there are any tools available to acheive the same.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
How about using simple variable placeholders like <some-element>@myvariable@</some-element> and run a brutal search-and-replace over the whole document? That should work regardless of how the template is changed as long as the expected variable placeholders are found somewhere within the document. Similarly, you wouldn't need to touch the code unless you change the way you mark-up your variable placeholders.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generating XML from template file.