Converting HL7 to XML

Introduction

 

For those of us unfortunate enough to have to deal with it, HL7 is a commonly encountered language used by medical systems to communicate with each other. It is what someone dreamed up way back when before XML was invented. Indeed, the most recent version of HL7 is XML. However, for those of us that still have to use older systems, it's an unwieldy and unfriendly language to deal with; message components are delimited using carriage returns, pipe symbols, tildes, and ampersands.

Since most applications use XML for data exchange, and XML is much nicer to deal with anyway, it would be helpful if there were an HL7 to XML conversion library that was freely available. Sadly, despite scouring the web, I have not found a (free) class or utility that can easily be integrated into applications. There are a few Java libraries, and one well known (excellent) commercial application, but nothing free and easy to use.

This article is v1 of my attempt at creating such a library. Please feel free to use and extend it - and most importantly, fix any bugs I've missed.

 

Using the code

A very simple HL7 message looks something like this:

MSH|^~\&|||||20080925161613||ADT^A05||P|2.6

This class and method simply produces an XML representation of the same message. Note that this class isn't nearly clever enough to know what type of HL7 message it is converting - it merely creates an XML version of it. The point is that you can then use XPath to retrieve the segment you want to use since you know its location. The HL7 above returned by this method would look like this:

<HL7Message>
    <MSH>
        <MSH.0>MSH</MSH.0>
        <MSH.1>^~\&amp;</MSH.1>
        <MSH.2/>
        <MSH.3/>
        <MSH.4/>
        <MSH.5/>
        <MSH.6>20080925161613</MSH.6>
        <MSH.7/>
        <MSH.8>
            <MSH.8.0>ADT</MSH.8.0>
            <MSH.8.1>A05</MSH.8.1>
        </MSH.8>
        <MSH.9/>
        <MSH.10>P</MSH.10>
        <MSH.11>2.6</MSH.11>
    </MSH>
</HL7Message>

It's a static class which returns an XML string (as a string; it could easily be modified to return an XmlDocument instead).

 

Using the class

string sHL7asXml = HL7ToXmlConverter.ConvertToXml(myHL7string);

The full class looks like this:

posted @ 2010-07-01 17:45  Max Woods  阅读(663)  评论(0编辑  收藏  举报