Parsing a HL7 Batch File

This example will show you how to implement the processing of a batch HL7 file. You will need a main class, as well as a listener class.

The first thing we will implement is the listener class. The class will do the message processing. It will receive every message in the batch file, and it will print out the patient's name (PID-5.1, PID-5.2), and their MRNs (PID-3.1).

The listener must implement HL7MessageListener, which is a single method: boolean messageReceived(HL7Structure struct).

Now that the listener is implemented, we can write the code to call it. We just need to create an instance of our listener, and then pass it as a parameter to SparkFileReader, along with a path to the file. Once the SparkFileReader is created, we just need to call .parse() to begin parsing the batch file. We must use a try/catch for this call because it may throw an IOException if an IO problem occurs.

It's that simple. The file specified in MyMainClass will be parsed, and each message, one by one will be passed to PrintPatientListener. The HL7Structure that is passed can be immediately accessed and used to traverse the HL7 message... Any HL7 message.