ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Reilly <>
Subject Re: XML Parsing Question
Date Thu, 07 Oct 2004 14:24:36 GMT
Brent Bain wrote:

>I've tried the task that Peter Reilly attached to one of the emails about a
>month ago for an XPath iterator but I can't seem to get it to work with the
>current ant-contrib tasks.
I tested it with the latest ant-contrib - 1.0b1, you may have an older 
version in your path.
Also, it uses the java 1.5 xpath api.

If you just want to run the scripts, and not extract the names, ips or 
jvm names, you can do:
    <macrodef name="startengine">
      <attribute name="script"/>
        <echo>Start a machine using script @{script}</echo>
    <typedef name="xpathiter" classname="XPathIterator"
    <ac:for param="script">
      <xpathiter expression="/servers/server/jvm/scripts/jvmStart/text()"
                 file="machines.xml" trim="yes"/>
        <startengine script="@{script}"/>

If you want to access the names and jvms, things are a little more 
You will need to do two loops, one over the servers, and an inner one over
the jvms.

You may be able to use xmltask for this or you could use a script to do it.
For example with beanshell and java1.5 one can do this:

    <macrodef name="startengine">
      <attribute name="name"/>
      <attribute name="ip"/>
      <attribute name="jvm"/>
      <attribute name="script"/>
        <echo>Start on @{name} (@{ip}) jvm @{jvm} script @{script}</echo>

    <macrodef name="stopengine">
      <attribute name="name"/>
      <attribute name="ip"/>
      <attribute name="jvm"/>
      <attribute name="script"/>
        <echo>stop on @{name} (@{ip}) jvm @{jvm} script @{script}</echo>

    <scriptdef name="runscript" language="beanshell">
      <attribute name="macro"/>
      <attribute name="file"/>
      <attribute name="script"/>
      import javax.xml.xpath.*;
      import org.xml.sax.*;
      import org.w3c.dom.*;
      import java.util.*;
      xpath = XPathFactory.newInstance().newXPath();

      getString(obj, exp) {
          return  xpath.evaluate(exp, obj, XPathConstants.STRING);
      getNodes(obj, exp) {
           // Convert to a list
           ret = new ArrayList();
           nodes = xpath.evaluate(exp, obj,  XPathConstants.NODESET);
           for (i = 0; i < nodes.getLength(); ++i) {
           return ret;
      try {
         source = new InputSource(attributes.get("file"));
         for (server : getNodes(source, "/servers/server")) {
             name =  getString(server, "name/text()");
             ip =    getString(server, "ip/text()");
             for (jvm : getNodes(server, "jvm")) {
                 jvmName = getString(jvm, "@name");
                 startScript = getString(jvm, "scripts/" + 
attributes.get("script") + "/text()");
                 macro = project.createTask(attributes.get("macro"));

                 macro.setDynamicAttribute("name", name);
                 macro.setDynamicAttribute("ip", ip);
                 macro.setDynamicAttribute("jvm", jvmName);
                 macro.setDynamicAttribute("script", startScript);

      } catch (Exception ex) {

    <runscript file="machines.xml" script="jvmStart" macro="startengine"/>
    <runscript file="machines.xml" script="jvmStop" macro="stopengine"/>

resulting in:
     [echo] Start on Machine1 ( jvm JVM1 script
     [echo] Start on Machine1 ( jvm JVM3 script
     [echo] Start on Machine2 ( jvm JVM2 script
     [echo] stop on Machine1 ( jvm JVM1 script
     [echo] stop on Machine1 ( jvm JVM3 script
     [echo] stop on Machine2 ( jvm JVM2 script


>  I've also thought about doing this with the xslt
>task but that also seems like a headache (I can get a comma delimited
>property for the different jvm names on a given server and then another xslt
>step to actually pull out the script names that I will then execute -- but
>that just seems clunky).
>Has anyone else run into this type of problem or have some other solution
>that I'm missing?  Any recommendations on the xml document (I can get IT to
>change it if needed).  The number of servers/jvms will change depending on
>any number of factors so it's not always just going to be 2 machines and 3
>In advance, my hair thanks you (I've been pulling it out all morning trying
>to grasp how to handle this!).

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message