Original link http://blog.csdn.net/ycyangcai/article/details/6643784
The goal of the Hl7 engine is primarily to address the standardization of data in accordance with the requirements of the HL7 protocol, the integration of standard services and the synchronization of standard business data between different systems. In the years of hospital informatization process,HL7 Standard Organization and analysis of the most complex, the following is I used for many years HL7 engine analysis, mainly has two version 1. C #,2. The Java version .
This time the open Java
Engine class: Package Com.xxxx.hl7;import java.io.fileoutputstream;import Java.io.ioexception;import Java.io.outputstreamwriter;import Java.io.writer;import Java.util.list;import Org.dom4j.Document;import Org.dom4j.documenthelper;import Org.dom4j.element;import Org.dom4j.node;import Org.dom4j.io.OutputFormat;import Org.dom4j.io.xmlwriter;public class Hl7toxmlconverter {public static string Converttoxml (String sHL7) {Document Document = Converttoxmlobject (sHL7); String hl7str = Document.asxml (); return hl7str;} public static String Converttoxml (document document) {string hl7str = Document.asxml (); return hl7str;} public static document Converttoxmlobject (String sHL7) {Document document = Createxmldoc ();//divide HL7 into segments string[] Shl7lines = Shl7.split ("\ n");//Remove the XML keyword for (int i = 0; i < shl7lines.length; i++) {Shl7lines[i] = Shl7lines[i].replace ("^~\\& "," "). Replace (" MSH "," msh| ");} for (int i = 0; i < shl7lines.length; i++) {//Determine if empty line if (shl7lines[i]! = null) {String shl7line = shl7lines[i];//via/r or/n EnterDelimiter string[] Sfields = Getmessgefields (shl7line);//Create a first-level node element el = Document.getrootelement () for a segment (row). AddElement ( Sfields[0]);//Cycle each line of Boolean ismsh=true;for (int a = 1; a < sfields.length; a++) {//whether to include HL7 connector ^~\\&if (sfields[a].i Ndexof (' ^ ') >0 | | Sfields[a].indexof (' ~ ') >0 | | Sfields[a].indexof (' \ \ ') >0 | | Sfields[a].indexof (' & ') >0) {//0: If this line has any separator//start operation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//through ~ Delimited string[] scomponents = getrepetitions (Sfields[a]); if (Scomponents.length > 1) {//1: If you can separate 0001^ Guo Jing ^ medical number ^eq^and~0002 ^ East Zone ^ Ward number ^eq^andfor (int b = 0; b < scomponents.length; b++) {//Element FIELDEL1 = El.addelement (Sfields[0] + "." + a); C Reatecomponents (EL,SCOMPONENTS[B],SFIELDS[0],A,B);}} ELSE{//1: If there is really only one value of 0001^ Guo Jing ^ Medical number ^eq^and//Create a second level node for the field//Element Fieldel = El.addelement (Sfields[0] + "." + a); Createcomponents (el,sfields[a],sfields[0],a,0);//fieldel.settext (sfields[a]+ "11111111111111");} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}else{//0: If this line does not have any separators//Create a second-level node for the field element Fieldel = El.addelement (Sfields[0] + "." + a); Fieldel.settext (Sfields[a]);}} End If}//end for//modifies the value of Msh.1 and msh.2 Document.selectsinglenode ("Hl7message/msh/msh.1"). SetText ("|"); Document.selectsinglenode ("Hl7message/msh/msh.2"). SetText ("~^\\&");//Document.selectnodes ("Msh/msh.1"); return document;} @SuppressWarnings ("unused") private static Element createcomponents (final Element el,final String hl7components,string Sfield,int A,int b) {element Componentel = el.addelement (Sfield + "." + a);//Element Componentel =el;//.addelement (Sfield + "." + A + "." + b);//&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&& &&&&&&//through & separates string[] subcomponents = getsubcomponents (hl7components); Subcomponents.length > 1) {//2. If there is a word group, there is generally no ... Sub-group & seldom use}ELSE{//2. If not, use ^ Group string[] Srepetitions = GetcompoNents (hl7components); if (Srepetitions.length > 1) {Element Repetitionel = null;for (int c = 0; C < srepetitions.lengt H C + +) {Repetitionel = componentel.addelement (Sfield + "." + A + "." + (c+1)); Repetitionel.settext (Srepetitions[c]);}} Else{componentel.settext (hl7components);}} &&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&& &&&return el;} <summary>///by | Separating the fields///</summary>///<param name= "s" ></param>///<returns></ Returns>private Static string[] Getmessgefields (String s) {return s.split ("\\|");} <summary>///by ^ delimited Group field///</summary>///<param name= "s" ></param>///<returns></ Returns>private Static string[] getcomponents (String s) {return s.split ("\\^");} <summary>///the Sub Group field by &//</summary>///<param name="S" ></param>///<returns></returns>private static string[] Getsubcomponents (String s) {return S.split ("&");} <summary>///through ~ delimited repeat///</summary>///<param name= "s" ></param>///<returns></ Returns>private Static string[] Getrepetitions (String s) {return s.split ("~");} <summary>///creating an XML object///</summary>///<returns></returns>private static Document Createxmldoc () {Document output = documenthelper.createdocument ();//Generate a contact element RootNode = output.addelement (" Hl7message "); return output;} public static string GetText (document document, string path) {node node = document.selectsinglenode ("hl7message/" +path); if (node! = null) {return node.gettext ();} Else{return null;}} public static string GetText (document document, String Path,int index) {List nodes = document.selectnodes ("hl7message/" + Path), if (Nodes!=null) {return (Node) nodes.get (Index)). GetText (); Else{return null;}} public static List gettexts (Document documENT, String path) {List nodes = document.selectnodes ("hl7message/" +path); return nodes;} public static void Writedocument (document document, String filepath) {try{//read file//FileWriter FileWriter = new FileWriter ( filepath); Writer writer = new OutputStreamWriter (new FileOutputStream (filepath), "utf-8");//Set file encoding OutputFormat Xmlformat = new OutputFormat (); Xmlformat.setencoding ("Utf-8");//Create Write file method XmlWriter XmlWriter = new XmlWriter (writer,xmlformat);// Write file Xmlwriter.write (document);//close Xmlwriter.close ();} catch (IOException e) {System.out.println ("File not Found"); E.printstacktrace ();}} Unit Test Call class: String myhl7string= "msh|^~\\&|455755610_0100| | 0200| | 20110624160404|000| qry^a19^qry_a19|0123456001| p|2.6\nqrd| | | | | | | | | 0001^ Guo Jing ^ medical number ^eq^and~0002^ East Zone ^ Ward number ^eq^and\nqrf| | 20110627|20110803 ";D ocument Document = Hl7toxmlconverter.converttoxmlobject (myhl7string);//Get event String eventName = Hl7toxmlconverter.gettext (document, "msh/msh.9/msh.9.3"); System.out.println ("EventName:" +eventname);//List NodeValue = Document.selectnoDes ("msh.1"); String nodevalue = Document.selectsinglenode ("Hl7message/msh/msh.1"). GetText (); String nodeValue2 = Document.selectsinglenode ("hl7message/msh/msh.3"). GetText ();//Documentelement.selectnodes ( path); System.out.println (nodevalue+ ":" +nodevalue2); String value = Hl7toxmlconverter.gettext (document, "qrd/qrd.9/qrd.9.1", 0); String value1 = hl7toxmlconverter.gettext (document, "qrd/qrd.9/qrd.9.1", 1); String value2 = hl7toxmlconverter.gettext (document, "qrd/qrd.9/qrd.9.1"); System.out.println (value+ ":" +value1+ ":" +value2); list<node> list = hl7toxmlconverter.gettexts (document, "qrd/qrd.9/qrd.9.1"); for (Node node:list) { System.out.println (":" +node.gettext ());} System.out.println (Hl7toxmlconverter.converttoxml (myhl7string));
HL7 Java Parsing