tags:

views:

68

answers:

5

I am quite new to ant and have been looking at the tasks. I am trying to generate an xml file. Do I need to call an external process or does ant have some way to do this? It could be as simple as sending a string to a txt file and saving it as a .xml. Is it possible?

A: 

You could do that, although it seems to me more like something you would want to script beforehand, or something that you could call out to from Ant.

The task would be:

<echo file="my.xml"><!--put escaped xml here--></echo>

This is of course going to be tedious since everything has to be escaped, but you can do it.

Jordan
A: 

If you look at this it will allow you to run a Java program from Ant.

Romain Hippeau
A: 

You can use the Replace task to replace a given string in a template, and save it as xml. A simple example, as seen in the ant documentation:

<replace file="${src}/index.html" token="@@@" value="wombat"/>

I used it to replace a constant @version@ by the actual build identifier in a java project, for example.

If you want to do more complex processing, you should look at the XSLT task. Foe example, to generate documentation, with the date replace in the output, you can use something like:

<xslt basedir="doc" destdir="build/doc"
      extension=".html" style="style/apache.xsl">
  <param name="date" expression="07-01-2000"/>
</xslt>
tonio
+1  A: 

The correct answer depends upon what you're actually trying to do.

You could create a tiny xml document using echo task and argument replacement, but that gets hard to maintain very quickly.

If your goal is to generate an XML document that is mostly boilerplate with a couple of values substituted, then you should look at creating a template document and then using the Copy with filtering task.

If you need to modify the structure of the document depending upon data from Ant (or gathered from somewhere else) then using the xslt task is going to be a better fit. The problem with XSLT is that it is not always straight-forward to use (XSLT uses functional programming, not procedural programming).

You may find that the correct answer is to write your own Ant task that will do exactly what you want, just the way you want it.

If you can better describe what you're trying to achieve, I'm sure someone will be happy to provide a more precise answer.

Craig Trader
Thanks, I went with copying with filtering, works very nicely!
sarcasteak
A: 

When you have to deal with any kind of XML processing within in your ant workflow, the xmltask is strongly recommended. Very detailed documentation and good support. All you need beside that is some XPATH knowledge. Here => another helpful article

Regards, Gilbert

Rebse