5. DB Conversion

A database converter exports stored HL7v3 messages or message fragments into XML statements.

This requires similar knowledge about the HL7v3 fragment that was required to store it in the first place, see also Message Conversion:

  • A description of the used database tables as defined by the RIM.
  • A description of the datatypes defined in this RIM.
  • A description of the cloned classes used in the message.
  • A parser capable of reading the xml version of the message.

The descriptions and parser are generated from the model that describes the message.

The dbconvertor takes these inputs, a source database, and the id of the encompassing class to reconstruct the source message. Here is a minimal example program that reconstitutes previously read CDA R2 messages:

from mgridenv import log
import sys
import psycopg2

from lib.dbconverter import DBConverter
from lib.graft import improvexmldtout

import generated.rim.rim210 as rim
from generated.rim.rim210_dt import xml2dt as datatypes
import generated.mif.CDA_R2_NE2010.POCD_MT000040
import generated.parser.CDA_R2_NE2010.CDA_parser as outparser

knownmt = ['POCD_MT000040']

(database, user, cdaid) = sys.argv[1:]

conn = psycopg2.connect('host= dbname=%s user=%s' % (database, user))
cur = conn.cursor()

converter = DBConverter(cursor = cur, log = log, parser = outparser, rim = rim, \
                        datatypes = datatypes, knownmt = knownmt)
obj = converter.retrieveobj('POCD_MT000040', 'ClinicalDocument', int(cdaid))
cobj = converter.convert(mifname = 'POCD_MT000040',
                         mifclass = 'ClinicalDocument', obj = obj)
print converter.export(xmlname = 'ClinicalDocument', indent = 0, \
                       obj = cobj, namespacedef='xmlns="urn:hl7-org:v3"')

5.1. Process

DBConversion process as seen in the example above:

  • Connect to a database.
  • Initialize a DBConverter instance, providing generated models.
  • Retrieve a MIF instance using a database cursor, the MIF name, the clone name and a database id. This step returns the MIF instance in a python dictionary. Relations to other objects are traversed, so when requesting a ClinicalDocument all enclosed Observations, Participations, Roles and the like are included.
  • Convert the object dictionary to parser defined document object model.
  • Export the document object model; this returns an xml string literal representing the MIF instance.


The readability of the xml output for some datatypes may be improved by using the graft module. Some HL7v3 concepts are hard to express in XML Schema, and lead to e.g. dummy attributes in some datatypes. These dummies would normally be exported, but can be surpressed using datatype specific grafts.


The current DBConverter does not yet support the export of messages that were inserted as JSON statements.