"...probably the best custom task around..."

XmlTask mailing list, November 2005


"...too essential to be left out of the Ant distribution..."

Ant User mailing list, August 2002

New Product! OOPS Consultancy has just released Veduta 1.1 - the innovative, intuitive and unique means to view complex system logs

Download a trial edition today!

Contents
Introduction
Recent Changes
Download
Support
Usage [+]

Buffers
Formatting
Examples
Known Issues
XPath Links
Contact

Introduction
xmltask provides the facility for automatically editing XML files as part of an Ant build. Unlike the standard filter task provided with Ant, it is XML-sensitive, but doesn't require you to define XSLTs.

Uses include:

"Keep up the good work - xmltask is invaluable to our promotion process"
User comment, June 2003

Top

Recent Changes
Read the new tutorial on <xmltask> at java.net

Current version 1.16

See the CHANGES file in the download for a comprehensive list of changes for each version

Top

Download
Version 1.16 - release 22-Sep-2009

xmltask is released under the Apache license. Right-click and choose Save As if your browser doesn't offer you the option.

xmltask.jar - the .jar file to use in Ant.

xmltask.tar.gz - the source code and tests

Checksums:
xmltask.jar - MD5 b2cda8a5800858d503a8b11253a3e642
xmltask.tar.gz - MD5 bdd9815475018b8b4dbcfde08319ebe2

Download previous versions at Sourceforge.net

Top

How To Use
To use this task, make sure:

The xmltask.jar is in your $CLASSPATH

Reference the xmltask in your build.xml eg.

<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>

Reference the xmltask task as part of your build eg.

<target name="main">

<xmltask source="input.xml" dest="output.xml">

...

Top

Support
xmltask now has a support mailing list.

Email xmltask-users@lists.sourceforge.net

To subscribe, or view the archives, visit the Xmltask-users mail page

Top

Usage
xmltask allows you to specify sections of an XML file to append to, replace, remove or modify. The sections of the XML document to be modified are specified by XPath references, and the XML to insert can be specified in-line in the Ant build.xml, or loaded from files.

Top

Buffers
Buffers are used to store nodes found by <cut> and <copy> operations, and those nodes can be inserted into a document using <insert> / <paste>.

Buffers exist for the duration of the Ant process and consequently can be used across multiple invocations of <xmltask>. eg. the following is possible:

  <target name="cut">
    <xmltask source="input.xml" dest="1.xml >
      <cut path="web/servlet/context/config[@id='4']" buffer="storedXml" />
    </xmltask>
  </target>

  <target name="paste" depends="cut">
    <xmltask source="input.xml" dest="output.xml >
      <paste path="web/servlet/context/config[@id='5'] buffer="storedXml" />
    </xmltask>
  </target>
  
so the buffer storedXml is maintained across multiple targets.

Buffers are simply defined by names. eg. valid buffers would be servlet, buffer100 etc.

A buffer can record multiple nodes (either resulting from multiple matches or multiple <cut> / <copy> operations). This operation is enabled through use of the append attribute. e.g.

<cut path="web/servlet/context/config" buffer="storedXml" append="true" />

A buffer can store all types of XML nodes e.g. text / elements / attributes. Note that when recording an attribute node, both the name of the attribute and the value will be recorded. To store the value alone of an attribute, the attrValue attribute can be used e.g.

<copy path="web/servlet/@id" buffer="id" attrValue="true" />

This will store the value of the id attribute. The value can be used as a text node in a subsequent <insert> / <paste>.

Buffers can be persisted to files. This permits buffers to be used across Ant invocations, and uses of <antcall>. To persist a buffer to a file, simply name it using a file URL. e.g.

        <cut path="/a/b" buffer="file://build/buffers/1"/>
      
and the operation will write the cut XML to a file build/buffers/1. This file will persist after Ant exits, so care should be taken to remove this if required. The file will be created automatically, but any directories required must exist prior to the buffer being used.

Top

Formatting
The formatting of the output document is controlled by the attribute 'outputter'. There are three options:

<xmltask outputter="default"...
outputs the document as is. That is to say, all whitespace etc. is preserved. This is the default option. Note that attribute ordering may change and elements containing attributes may be split over several lines etc. ie. the document remains the same semantically.

<xmltask outputter="simple"...
outputs the document with a degree of formatting. Elements are indented and given new lines wherever possible to make a more readable document. This is not suitable for all applications since some XML consumers will be whitespace sensitive.

Spacing can be adjusted by using <xmltask outputter="simple:{indent}...>". e.g. <xmltask outputter="simple:1"... results in:

    <root>
     <branch/>
    </root>
  
The indent level can be increased: <xmltask outputter="simple:4"... results in:
    <root>
        <branch/>
    </root>

<xmltask outputter="{classname}"...
outputs the document using the nominated class as the outputting mechanism. This allows you to control the output of the document to your own tastes. The specified class must:

The custom class will be loaded and instantiated, then passed to a javax.xml.transform.sax.SAXResult object. Hence the outputter object will receive SAX events for each node in the resultant XML document. Note:

A simple introduction is to look at the com.oopsconsultancy.xmltask.output.FormattedDataWriter source code (in the source tarball).

Top

Examples
Some examples of common usage:

Top

Known Issues
The Java 1.4.2 release as of June 2003 has tightened up XPath parsing and what is regarded as acceptable XPath syntax. In particular, the following usage of trailing path separators is now regarded as incorrect:

/root/branch/

and should be replaced with

/root/branch

Some of the xmltask examples and documentation have used the incorrect syntax. This is now rectified.

Top

XPath Links
The XPath spec can be found here

An excellent XPath tutorial can be found here

Hints and tips on XPath can be found at O'Reilly's XML web site

Many Xpath issues relate to namespace matching. This XML.com article offers an excellent discussion of the issues.

The following books are invaluable for XPath issues:

XPath and XPointer by John E. Simpson

 

XSLT by Doug Tidwell. Contains
useful XPath information

Top

Contact
xmltask is written and maintained by OOPS Consultancy Ltd.. For enquiries see the support info.

Now hosted on

SourceForge Logo

Copyright © 1997-2006 OOPS Consultancy Ltd