1

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
Community
  • 1
  • 1
bernie2436
  • 22,841
  • 49
  • 151
  • 244

2 Answers2

3

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
Chad
  • 141
  • 1
  • 2
  • I have a reference to the dll set up and am able to reference things like pipe parser. But when I try NHapi.Model.V23.Message.QRY_R02 myQryR2 I get the error: The type or namespace NHapi does not exist in the namespace NHapi.Base.Model When I open the QRY_R02.cs file, however, it says the namespace is NHapi.Model.V251.Message I am confused. Are there two DLLs that I should reference? – bernie2436 May 25 '12 at 19:48
  • There are multiple NHapi.Model.XXXX versions. Basically, if you reference the NHapi.Model.V23, then it will bring in all of the HL7 2.3 models. If you reference NHapi.Model.V25, then it will bring in all of the HL7 2.5 models. You will only want to reference one of the NHapi.Model.XXX dlls. You should only need to reference the NHapi.Base.dll and the NHapi.Model.XXX.dll. – Chad May 31 '12 at 02:55
0

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

men
  • 131
  • 1
  • 9