Search code examples
hl7hapihl7-v2

HAPI: Hl7 Parser do not separate segments, appending all data in one HL7 Segment


I am simply trying to create a HL7 V2.6 message by using HAPI API. I am using latest 2.2 API.

When I try to print message, all data appending in one segment only. Here is my code-

package com.psl;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.v26.message.ADT_A01;
import ca.uhn.hl7v2.model.v26.segment.MSH;
import ca.uhn.hl7v2.model.v26.segment.PID;
import ca.uhn.hl7v2.parser.Parser;

/**
           * Example transmitting a message
           * 
           * @author <a href="mailto:[email protected]">James Agnew</a>
           * @version $Revision: 1.4 $ updated on $Date: 2009-10-03 15:29:05 $ by $Author: jamesagnew $
           */
          public class CreateAMessage
          {

              /**
               * @param args
               * @throws HL7Exception 
               */
              public static void main(String[] args) throws Exception {

                 ADT_A01 adt = new ADT_A01();
                  adt.initQuickstart("ADT", "A01", "P");

                  // Populate the MSH Segment
                  MSH mshSegment = adt.getMSH();
                  mshSegment.getSendingApplication().getNamespaceID().setValue("TestSendingSystem");
                  mshSegment.getDateTimeOfMessage().setDatePrecision(2016,06,15);
                  mshSegment.getMsh10_MessageControlID().setValue("12345");
                  mshSegment.getSequenceNumber().setValue("123");
                  mshSegment.getAcceptAcknowledgmentType().setValue("AL");;


                  // Populate the PID Segment
                  PID pid = adt.getPID(); 
                  pid.getPatientName(0).getFamilyName().getSurname().setValue("Doe");
                  pid.getPatientName(0).getGivenName().setValue("John");
                  pid.getPatientIdentifierList(0).getCx1_IDNumber().setValue("1234567"); 

                 /*
                   * In a real situation, of course, many more segments and fields would be populated
                   */

                  // Now, let's encode the message and look at the output
                  HapiContext context = new DefaultHapiContext();
                  Parser parser = context.getPipeParser();
                  String encodedMessage = parser.encode(adt);
                  System.out.println("Printing ER7 Encoded Message:");
                  System.out.println(encodedMessage);

                  /*
                   * Prints:
                   * 
                   * MSH|^~\&|TestSendingSystem||||200701011539||ADT^A01^ADT A01||||123
                   * PID|||123456||Doe^John
                   */

                  // Next, let's use the XML parser to encode as XML
                  parser = context.getXMLParser();
                  encodedMessage = parser.encode(adt);
                  System.out.println("Printing XML Encoded Message:");
                  System.out.println(encodedMessage);

    }

}

Console Output:

console output: PID|||1234567||Doe^Johntem||||20160615||ADT^A01^ADT_A01|12345|P|2.6|123||AL

No MSH? Please help.

When I print V2 XML message, it comes out correctly. Could someone please provide help in resolving issue.

I have taken this example from HAPI website- http://hl7api.sourceforge.net/xref/ca/uhn/hl7v2/examples/CreateAMessage.html


Solution

  • It seems your println prints all segments in one line and so overwriting the MSH segment with the PID segment. I guess you use Windows.

    You can see parts of the MSH segments in your output. Try to either adjust your terminal program so, that it also makes a newline / linefeed after just a carriage return or use a different console program.

    You could also printf into a file and inspect the output with an editor that works just with carriage return.