Ant: Iterating through an XML file's nodes

Go To StackoverFlow.com

2

I have the following XML structure:

<servers>
    <hostname>ABC01</hostname>
    <hostname>ABC02</hostname>
</servers>

I need to retrieve a file from each server, from a folder I know, attach it to an email and then send it.

What would be the approach for this?

Thanks.

2012-04-03 20:55
by bruneti12


2

Use the xmlproperty task to load an XML file into properties.

Then use the for task from ant-contrib to act upon each of the matched properties.

Something like:

<target name="funtimes">
    <xmlproperty file="the.xml" delimiter=","/>
    <for list="${servers.hostname}" param="hostname">
        <sequential>
            <echo>Doing things with @{hostname}</echo>
        </sequential>
    </for>
</target>

Fetching files depends upon how you are planning to access them. The scp task might help.

For sending email you can use the mail task.

2012-04-03 23:42
by Synesso
What I have in mind for fetching the files is copying them one by one to a local shared folder using the for task which you explained me previously. And then, in the mail task, attaching them to the email putting them all together in a FileSet. I'm new at Ant and I'm doing some research to see if this is possible :) - bruneti12 2012-04-04 12:45