6

I need to parse HL7 message ,firstly ,validate the message and then parse.

 XMLParser xmlParser = new DefaultXMLParser();

 //encode message in XML 
 String hl7MessageInXML = null;
 try {
        hl7MessageInXML = xmlParser.encode(message);
 } catch (HL7Exception e) {
    e.printStackTrace();
 }
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
MaNn
  • 745
  • 4
  • 11
  • 22
  • This works for : MSH|^~\&|Meg|XYZHospC|SurOE|XYCtr|20060529090131-050||ADT^A01^ADT_A01|01052901|P|2.5 EVN||200605290901||||200605290900 PID|||56782445^^^UAReg^PI||KLEINSAMPLE^BARRY^Q^JR||19620910|M||2-9^^HL70005^RA93^^XYZ|260 GOODWIN CREST DRIVE^^BIRMINGHAM^AL^35 209^^M~NICKELL’S PICKLES^10000 W 100TH AVE^BIRMINGHAM^AL^35^^O |||||||0105I30001^^^99DEF^AN PV1||I|W^389^1^UABH^^^^3||||12345^MORGAN^REX^J^^^MD^0010^UAMC^L||678 90^GRAINGER^LUCY^X^^^MD^0010^UAMC^L|MED|||||A0||13579^POTTER^SHER MAN^T^^^MD^0010^UAMC^L|||||||||||||||||||||||||||200605290900 OBX|1|NM|^Body Height||1.80|m^Meter^ISO+|||||F – MaNn Dec 27 '12 at 06:30
  • [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Ajinkya Dec 27 '12 at 06:30
  • good for you, where's the question? – Denis Tulskiy Dec 27 '12 at 06:30
  • Its not working for other HL7 messages – MaNn Dec 27 '12 at 06:59

2 Answers2

3

Example code:

import ca.uhn.hl7v2.parser.*;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.message.ACK;

public class ParserDemo {

    public static void main(String args[]) {
        //for demo purposes, we just declare a literal message string 
        String ackMessageString 
            = "MSH|^~\\&|foo|foo||foo|200108151718||ACK^A01^ACK|1|D|2.4|\rMSA|AA\r";

        //instantiate a PipeParser, which handles the "traditional encoding" 
        PipeParser pipeParser = new PipeParser();

        try {
            //parse the message string into a Message object 
            Message message = pipeParser.parse(ackMessageString);

            //if it is an ACK message (as we know it is),  cast it to an 
            // ACK object so that it is easier to work with, and change a value            
            if (message instanceof ACK) {
                ACK ack = (ACK) message;
                ack.getMSH().getProcessingID().getProcessingMode().setValue("P");
            }

            //instantiate an XML parser 
            XMLParser xmlParser = new DefaultXMLParser();

            //encode message in XML 
            String ackMessageInXML = xmlParser.encode(message);

            //print XML-encoded message to standard out
            System.out.println(ackMessageInXML);
        } catch (Exception e) {
            e.printStackTrace();
        }       
    }
}
Sher
  • 116
  • 2
1

Try using HAPI Parser api (http://hl7api.sourceforge.net/). Here you can find some examples about parsing HL7 messages:

http://ignaciosuay.com/how-to-use-hapi-terser-with-hl7/

http://ignaciosuay.com/how-to-set-repetitions-in-hl7-messages-using-hapi-terser/

Hope it helps

ignacio.suay
  • 737
  • 6
  • 7
  • can terser extract string from a field with local or international customizations such as this segment of a HL7 v2.3 REF mesaage: PRD|PP|See T Tan^""^""^^""|""^^^^""^New Zealand||(08)569-7555||14134^NZMC – iceman Oct 29 '14 at 01:32
  • yes you can! You ca get any piece of information in an HL7 message. In your case, try terser.get("/PRD-" + component). For instance: terser.get("/PRD-1) == "PP". Please, let me know if you find any problem accessing this segment. – ignacio.suay Oct 31 '14 at 11:49
  • thanks. i am trying to read a number of HL7 files in a directory and initiate the terser. Is there a way to determine which version(v2.3/v2.4) of the file it is from the MSH segment and initiate the appropriate class model and generate a XML structure of the entire HL7 file? – iceman Nov 04 '14 at 04:06
  • Hi! usually this information is in MSH-12, so you can retrieve this information like this: terser.get("/MSH-12"). I would strongly recommend you to have a look to the documentation: http://hl7api.sourceforge.net/v23/apidocs/index.html Hope it helps. – ignacio.suay Nov 11 '14 at 12:50
  • Thanks. I have got a custom HL7 that I would like to parse. Is there any sample terser code to get started on this to convert this to a XML. What are the steps I need to follow? I checked the HAPI docs and for v2.3, this model doesn't exist in the API. http://pastebin.com/BwCTQ1LR – iceman Dec 01 '14 at 04:28