mardi 24 juin 2003

Modify an XML file with ANT

Recently, while using the wonderful Glue for building WebServices (WS), I came up with the following problem: how to modify an XML file with Ant? 
(The rationale of this is: when creating a new WS, you have to use a special deployment tool [a shell script] and manually patch one of the many generated XML files; and of course, I want to automate the "manually" part of the cycle! But I guess such a problem is more general and when using EJB, one could encounter same wondering). 
First, I rejected the idea of using an Ant task that deals with TEXTUAL modifications of regular text files: I simply could not see an XML file as a regular file! Let me tell you that I didn't even think about creating a dedicated task in Java for solving that particular problem: that would have been crazy! 

Then, I ended up with 2 ideas: 
 - using an XML Ant task (such as the xmltaskone) 
 - using a templating engine (Velocity or FreeMarker

I preferred the first option which fits nicely to my needs (design-to-cost approach ;-) and allow me to write XPaths expression to patch the original XML file. Great! 
Templating engine was ... tempting, but too far from both my real need and time-frame to achieve it! 

So as to conclude, one of my best friends and colleagues (best applies to both nouns!) would probably have envisioned the possibility to write an XSLT file to perform that stuff with variables passing and modification of the XML file: let's forgive him!

Aucun commentaire: