NMLParser - an XML - object serializer for python

Schema definition language

It is useful to have an XML specification of the object model - corresponding to the class definitions - as it is independent of a particular language, and can provide hints (such as types and restrictions) which are not always accessible by reflection from a language.

There are many different schema languages (XML-Schema, RELAX-NG, RDFS, XMI) but none of them is perfect for the task of defining object models. XML-Schema defines both an object model, and its mapping to particular XML elements, whereas we want just the object model definition. XMI, an XML encoding of UML diagrams includes more than we need.

Because of this we created a new simple schema language in XML, corresponding closely to class definitions in a programming language. This schema language is defined using itself below:

<Package name="org.neuroml.schema">
	<Class name="Package">
		<Field name="name" type="string"/>
		<Field name="desc" type="string"/>
		<Field name="classes" type="list" elementType="Class" xmlstyle="hidefield"/>
	<Class name="Class">
		<Field name="name" type="string"/>
		<Field name="desc" type="string"/>
		<Field name="extends" type="string"/>
		<Field name="fields" type="list" elementType="Field" xmlstyle="hidefield"/>
		<Field name="keyfield" type="string" default="name"/>
	<Class name="Field">
		<Field name="name" type="string"/>
		<Field name="type" type="enum" choice="int|string|double|boolean|url|list|ref|union|enum|any"/>
		<Field name="elementType" type="string"/>
		<Field name="choice" type="list" elementType="string" xmlstyle="compact"/>
		<Field name="desc" type="string"/>
		<Field name="default" type="string"/>
		<Field name="xmlstyle" type="enum" choice="compact|hidefield|showfield"/>
	<Class name="PackageList">
		<Field name="packages" type="list" elementType="Package" xmlstyle="hidefield"/>

and an example using it is given below

<Package name="">
  <Class name="Network">
	<Field name="states" type="list" elementType="State" xmlstyle="hidefield"/>
	<Field name="connections" type="list" elementType="Connection" xmlstyle="hidefield"/>
  <Class name="State">
	<Field name="name" type="string"/>
	<Field name="count" type="int"/>

  <Class name="Connection">
	<Field name="s1" type="ref" elementType="State"/>
	<Field name="s2" type="ref" elementType="State"/>
	<Field name="w" type="double"/>

... corresponding to Java class definitions:

class Network {
   List<State> states;
   List<Connection> connections;
class State {
   String name;
   int count;
class Connection {
   String s1;
   String s2;
   double w;


A package has a name, a description and a list of class definitions.


A class has a name, description, name of the class it extends, a list of fields, and an optional keyfield which could be used for referencing.

<Class name="X">
  <Field name="f1" type="int"/>
  <Field name="f2" type="string"/>
  <Field name="f3" type="object" elementType="Y"/>
  <Field name="f4" type="object" elementType="Y"/>
  <Field name="f5" type="list" elementType="Z"/>
  <Field name="f6" type="ref" elementType="A"/>

... corresponds to Java definition like:-
class X {
  int f1;
  String f2;
  Y f3;
  Y f4;
  List<Z> f5;
  Ref<A> f6;


A field has a type and a name (e.g. "int", "x"). Simple types are: int, string, double, boolean, url. List types and Reference types can specify an elementType. The xmlstyle attribute chooses whether lists are:

  • compact (values encoded in a single string like "item1|item2|item3")
  • showfield - the field name becomes a tag. e.g. values for field f5 in the example above encoded like:
  • hidefield - the field name tag is omitted. e.g. values for field f5 in the example above encoded like:

Python notes

The GNOSIS page on python and XML - describes xml_objectify, a python version which is elegant codewise but doesn't produce very beautiful XML.

The process:

  • (1) Write the class definitions in Python
  • (2) Write a schema XML file
  • (3) Call the built in methods in the kit to serialise / deserialise objects to/from memory.

Known issues

  • The "ref" type (a serialisable reference to another object) is currently just treated as a string - it's up to the code to dereference strings.