views:

4115

answers:

2

I have an xml file where I need to comment out a whole piece of text with Ant.

There's this ant task

<replace file="${src.dir}/Version.as" token="@revisionPrana" value="${revision}"/>

that I use to replace words, but in my case I need to replace a whole block like this:

            <value>
    <object class="edumatic.backoffice.view.modules.NavigationModuleInfo">
     <property name="url"
      value="edumatic/backoffice/view/modules/support/ExamsNavigationModule.swf" />
     <property name="icon"
      value="edumatic/backoffice/view/modules/support/assets/book.png" />
     <property name="title" value="Assessments" />
     <property name="pluginID" value="EXAM" />
    </object>
   </value>
   <value>
    <object class="edumatic.backoffice.view.modules.ContentModuleInfo">
     <property name="url"
      value="edumatic/backoffice/view/modules/support/ExamsContentModule.swf" />
     <property name="pluginID" value="EXAM" />
    </object>
   </value>

Into

                <!--value>
        <object class="edumatic.backoffice.view.modules.NavigationModuleInfo">
         <property name="url"
          value="edumatic/backoffice/view/modules/support/ExamsNavigationModule.swf" />
         <property name="icon"
          value="edumatic/backoffice/view/modules/support/assets/book.png" />
         <property name="title" value="Assessments" />
         <property name="pluginID" value="EXAM" />
        </object>
       </value>
       <value>
        <object class="edumatic.backoffice.view.modules.ContentModuleInfo">
         <property name="url"
          value="edumatic/backoffice/view/modules/support/ExamsContentModule.swf" />
         <property name="pluginID" value="EXAM" />
        </object>
       </value-->

So, basically I need to comment out a whole block of XML. Can I do this with a replace task (putting the whole block in the attribute token and value doesn't really work)? Or is there a quick way to read in the xml with ant and delete some nodes and save the xml again?

Searching for and replace it by isn't an option because there are multiple value children and not all of them need to be commented out.

Adding a attribute like isn't an option either because the xml is being parsed by an IOC container (Prana). Maybe prana will ignore the id="1" but it still iss messy, and I don't like messy on the long term.

Thx, Lieven Cardoen aka Johlero

+4  A: 

If you can identify what is to be replaced through a regular expression, I recommend using the optional task replaceregexp. Here's the doc: http://ant.apache.org/manual/OptionalTasks/replaceregexp.html You can call it twice, one for the start tag and other for the end tag.

The regexp for replacing your can be a bit cumbersome, since you say you do not want to replace all tags, but I think this is the easiest way.

Another option would be to create a custom ant task to do what you want.

Miguel Ping
Lieven Cardoen
Managed to make it work with replaceregexp. Did have to search a while before realizing the s flag was needed! Thx!
Lieven Cardoen
Nice to hear you nailed it.
Miguel Ping
+2  A: 

If it is an XML file, then you could also call an XSLT transform

Mads Hansen
Indeed, hadn't thought about that (+ the fact that I don't know a thing about XSLT...).
Lieven Cardoen