title: HAPI: Hl7 Parser do not separate segments, appending all data in one HL7 Segment
body: <p>I am simply trying to create a HL7 V2.6 message by using HAPI API. I am using latest 2.2 API. </p>
  
  <p>When I try to print message, all data appending in one segment only. Here is my code-</p>
  
  <pre><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 &lt;a href="mailto:jamesagnew@sourceforge.net"&gt;James Agnew&lt;/a&gt;
             * @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|^~\&amp;|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);
  
      }
  
  }
  </code></pre>
  
  <p>Console Output:</p>
  
  <p>console output: PID|||1234567||Doe^Johntem||||20160615||ADT^A01^ADT_A01|12345|P|2.6|123||AL</p>
  
  <p>No MSH? Please help.</p>
  
  <p>When I print V2 XML message, it comes out correctly. Could someone please provide help in resolving issue.</p>
  
  <p>I have taken this example from HAPI website- 
  <a href="http://hl7api.sourceforge.net/xref/ca/uhn/hl7v2/examples/CreateAMessage.html" rel="nofollow">http://hl7api.sourceforge.net/xref/ca/uhn/hl7v2/examples/CreateAMessage.html</a></p>