How do i access the information in an hl7 message parsed with nHapi

2.8k views Asked by At

I am learning how to use nHapi. As many have pointed out, there's not much documentation. Following this doc I've been able to parse a message using the library. But I can't figure out how to access that message using an object model (which is what I really want nHapi to do). Essentially, I want to take an HL7 message as a string and access it using the object model, in the same way that LINQ to SQL takes a database record and lets you access it as an object. I found Parsing an HL7 without a priori messageType knowledge, but it seems to be about something else because the code in the post returns a string instead of an HL7 object (like I need). In the documentation I linked to above they seem to access the parts of a message using a "query"--but I can't find the materials to query IMessages in the library.

Here is the code I'm using, with a line showing what I want to do...

Imports NHapi.Base Imports NHapi.Base.Parser Imports NHapi.Base.Model

Module Module1

Sub Main()

    Dim msg As String = "MSH|^~\&|SENDING|SENDER|RECV|INST|20060228155525||QRY^R02^QRY_R02|1|P|2.3|QRD|20060228155525|R|I||||10^RD&Records&0126|38923^^^^^^^^&INST|||"
    Dim myPipeParser As PipeParser = New PipeParser()
    Dim myImsg As IMessage = myPipeParser.Parse(msg)
    Dim msgType As String = myImsg.GetStructureName
    Dim mySendingFacilityName As String = myImsg.getSendingFacility()  //this is what I want

End Sub
2

There are 2 answers

2
Chad On

Remember with HL7 messages that each segment has to end with a line return.

Also, you'll want to parse the message back to its actual type in order for the object model to be fully populated correctly (notice that when I used myPipeParser.Parse it was cast back to a QRY_R02 message type from the NHapi.Model.V23 Library). So the code should look something like this:

Imports NHapi.Model.V23.Message
Imports NHapi.Base.Parser
Imports NHapi.Base
Module Module1

Sub Main()
    Dim msg As String = "MSH|^~\&|SENDING|SENDER|RECV|INST|20060228155525||QRY^R02^QRY_R02|1|P|2.3" & vbNewLine & _
    "QRD|20060228155525|R|I||||10^RD&Records&0126|38923^^^^^^^^&INST|||"
    Dim myPipeParser As PipeParser = New PipeParser()
    Dim myImsg As QRY_R02 = myPipeParser.Parse(msg)
    Dim msgType As String = myImsg.GetStructureName
    Dim mySendingFacilityName As String = myImsg.MSH.SendingFacility.NamespaceID.Value
    Console.WriteLine(mySendingFacilityName)
    Console.ReadLine()

End Sub

End Module
0
men On

I know it was a very long time ago, however I was looking for this resource very recently and found that there is nearly no documentation on how to use this API. And excellent source of examples can be found in the test part of source code in the project NHapi.NUnit. Sources can be found here