Using the XMLCodec add-on

Author: Giovanni Caire (Telecom Italia)

Date: November 10, 2006

Java platform: Sun JDK 1.4 or later

JADE version 3.0 or later


This tutorial describes how to use the XMLCodec add-on in combination with the JADE support for content languages and ontologies to encode and decode content expressions (i.e. expressions that can be included in the content slot of ACL messages) according to an XML-based syntax.

Furthermore the add-on provides utility methods to export and import whatever ontological element (regardless of whether or not it represents a Content Element) to/from an XML format.


In order to install XMLCodec add-on the following steps must be performed:

  • Download the add-on from the add-ons area of the JADE web site
  • Unzip the XMLCodec package in the root directory of the jade distribution. A directory <jade-home>/add-ons/XMLCodec should be created, being <jade-home> the root directory of the jade distribution.
  • Add the XMLCodec.jar file included in the XMLCodec/lib directory to the CLASSPATH.


The XMLCodec add-on can be re-compiled by means of the following ant targets defined in the XMLCodec/build.xml file:

  • ant – compiles the XMLCodec classes
  • ant lib – creates the XMLCodec.jar archive in the lib directory
  • ant clean – removes the compiled classes and the XMLCodec.jar archive
  • ant examples – compiles the example classes

Using the XMLCodec add-on to encode/decode content expressions

The jade.content.lang.xml.XMLCodec class implemnts the jade.content.lang.Codec interface and can therefore be used as other JADE codecs such as the SLCodec or the LEAPCodec. The code below, where an agent informs another agent that John is the father of Bill and Mary using the PeopleOntology, shows an example (look at the Tutorial on content languages and ontologies available on the JADE web site for details about codecs and ontologies).

private Codec xmlCodec = new XMLCodec();
private Ontology onto = PeopleOntology.getInstance(); 
protected void setup() { 
    ACLMessage msg = new ACLMessage(ACLMessage.INFORM); 
    // Fill message receiver and other message slots 

    FatherOf fo = new FatherOf(); 
    Man m = new Man(); 
    List children = new ArrayList(); 
    m = new Man(); 
    Woman w = new Woman(); 
    try { 
        getContentManager().fillContent(msg, fo); 
    }    catch (Exception e) { 

If we sniffed the message prepared and sent by the code above and we printed its content we would see the following output.


Using the XMLCodec add-on to export/import ontological elements to/from XML form

Besides the methods of the jade.content.lang.Codec interface, the jade.content.lang.xml.XMLCodec class also provides utility methods that can be used to export/import whatever ontological element (i.e. whatever object whose class is associated to a schema in an ontology) to/from an XML format regardless of whether or not it represents a content element. As an example the code below shows how to export a DFAgentDescription to XML. It should be noticed that this feature does not involve the agent ContentManager and therefore there is no need to register neither the codec nor the ontologies with it.

DFAgentDescription dfd = new DFAgentDescription(); 
dfd.setName(new AID("a", AID.ISLOCALNAME)); 
ServiceDescription sd = new ServiceDescription(); 
ServiceDescription sd = new ServiceDescription(); 
try { 
  String s = xmlCodec.encodeObject(FIPAManagementOntology.getInstance(), dfd, true);
}catch (Exception e) { 

The output produced by the above code is as follows (the third parameter of the encodeObject() method set to true indicates that the XML must be indented).