The goal of the HL7 engine is to standardize data according to the requirements of the HL7 protocol, integrate standard businesses, and synchronize standard business data between different systems. During the Informatization Process of the hospital integration platform for many years, the HL7 standard organization and analysis are the most complicated. The following is how I used the HL7 engine Analysis for many years, because the company upgraded to use the hl73.0 version, I decided to save the hl72.x engine on my blog.
(Engine parsing class) Decode. CS
Using system; using system. collections. generic; using system. text; using system. XML; using system. text. regularexpressions; namespace COM. china. HL7 {// <summary> /// HL7 parser /// </Summary> Public static class hl7toxmlconverter {Private Static xmldocument _ xmldoc; /// <summary> /// convert the HL7 information into XML format. // separate the Order \ n, | ,~, ^, //// </Summary> /// <Param name = "shl7"> HL7 string </param> /// <returns> </returns> Public static string converttoxml (string shl7) {_ xmldoc = converttoxmlobject (shl7); Return _ xmldoc. outerxml;} public static xmldocument converttoxmlobject (string shl7) {_ xmldoc = createxmldoc (); // split HL7 into string [] shl7lines = shl7.split ('\ n '); // remove the XML keyword for (INT I = 0; I <shl7lines. length; I ++) {shl7lines [I] = r Egex. Replace (shl7lines [I], @ "[^-~] "," ") ;}For (INT I = 0; I <shl7lines. length; I ++) {// determine whether empty rows if (shl7lines [I]! = String. empty) {string shl7line = shl7lines [I]; // string [] sfields = hl7toxmlconverter is separated by/R or/N carriage returns. getmessgefields (shl7line); // creates the first-level node xmlelement El = _ xmldoc for the segment (ROW. createelement (sfields [0]); _ xmldoc. documentelement. appendchild (EL); // loop each row for (int A = 0; A <sfields. length; A ++) {// create the second-level node xmlelement fieldel = _ xmldoc for the field. createelement (sfields [0] + ". "+. tostring (); // whether to include the HL7 connector if (Sfields [a]! = @ "^ ~ \ & ") {// 0: if this line has any separator, // pass ~ Separate string [] scomponents = hl7toxmlconverter. getrepetitions (sfields [a]); If (scomponents. length> 1) {// 1: If you can separate for (INT B = 0; B <scomponents. length; B ++) {xmlelement componentel = _ xmldoc. createelement (sfields [0] + ". "+. tostring () + ". "+ B. tostring (); // use & to separate string [] subcomponents = getsubcomponents (scomponents [B]); If (subcomponents. length> 1) {// 2. if there is a word group, it is generally not... For (int c = 0; C <subcomponents. length; C ++) {// modified an error string [] subcomponentrepetitions = getcomponents (subcomponents [c]); If (subcomponentrepetitions. length> 1) {for (int d = 0; D <subcomponentrepetitions. length; D ++) {xmlelement subcomponentrepel = _ xmldoc. createelement (sfields [0] + ". "+. tostring () + ". "+ B. tostring () + ". "+ C. tostring () + ". "+ D. tostring (); subcomponentrepel. inne Rtext = subcomponentrepetitions [d]; componentel. appendchild (subcomponentrepel) ;}} else {xmlelement subcomponentel = _ xmldoc. createelement (sfields [0] + ". "+. tostring () + ". "+ B. tostring () + ". "+ C. tostring (); subcomponentel. innertext = subcomponents [c]; componentel. appendchild (subcomponentel) ;}} fieldel. appendchild (componentel);} else {// 2. if no word group exists, it is generally not... String [] srepetitions = hl7toxmlconverter. getcomponents (scomponents [B]); If (srepetitions. length> 1) {xmlelement repetitionel = NULL; For (int c = 0; C <srepetitions. length; C ++) {repetitionel = _ xmldoc. createelement (sfields [0] + ". "+. tostring () + ". "+ B. tostring () + ". "+ C. tostring (); repetitionel. innertext = srepetitions [c]; componentel. appendchild (repetitionel);} fieldel. appendchild (Componentel); El. appendchild (fieldel);} else {componentel. innertext = scomponents [B]; fieldel. appendchild (componentel); El. appendchild (fieldel) ;}} el. appendchild (fieldel);} else {// 1: if it cannot be separated, you can directly write the node value. Fieldel. innertext = sfields [a]; El. appendchild (fieldel) ;}} else {// 0: If the node values cannot be separated, you can directly write the node values. Fieldel. innertext = sfields [a]; El. appendchild (fieldel) ;}}} return _ xmldoc ;} /// <summary> /// use | to separate fields /// </Summary> /// <Param name = "S"> </param> /// <returns> </returns> Private Static string [] getmessgefields (string S) {return S. split ('| ');} /// <summary> /// use ^ to separate the group fields /// </Summary> /// <Param name = "S"> </param> // <returns> </returns> Private Static string [] getcomponents (string S) {r Eturn S. split ('^ ');} /// <summary> /// use & to separate the sub-group field /// </Summary> /// <Param name = "S"> </param> // /<returns> </returns> Private Static string [] getsubcomponents (string S) {return S. split ('&') ;}/// <summary> // pass ~ Duplicate separator /// </Summary> /// <Param name = "S"> </param> /// <returns> </returns> Private Static string [] getrepetitions (string S) {return S. split ('~ ');} // <Summary> // create an XML Object // </Summary> /// <returns> </returns> Private Static xmldocument createxmldoc () {xmldocument output = new xmldocument (); xmlelement rootnode = output. createelement ("hl7message"); output. appendchild (rootnode); Return output;} public static string gettext (xmldocument xmlobject, string path) {xmlnode node = xmlobject. documentelement. selectsinglenode (PATH); If (node! = NULL) {return node. innertext;} else {return NULL;} public static string gettext (xmldocument xmlobject, string path, int index) {xmlnodelist nodes = xmlobject. documentelement. selectnodes (PATH); If (index <= nodes. count) {return nodes [Index]. innertext;} else {return NULL;} public static string [] gettexts (xmldocument xmlobject, string path) {xmlnodelist nodes = xmlobject. documentelement. selectnodes (PATH); string [] arr = new string [nodes. count]; int Index = 0; foreach (xmlnode node in nodes) {arr [index ++] = node. innertext;} return arr ;}}}
Test method:
String myhl7string = "MSH | ^ ~ \\& | 455755610_0100 | 0200 | 20110624160404 | 000 | qry ^ A19 ^ qry_a19 | 0123456001 | p | 2.6 \ nqrd | ||||| | 0001 ^ Guo Jing ^ health Check number ^ EQ ^ and ~ 0002 ^ region 1 ^ EQ ^ and \ nqrf | 20110627 | 20110803 "; string shl7asxml = hl7toxmlconverter. converttoxml (myhl7string); xmldocument xmlobject = hl7toxmlconverter. converttoxmlobject (myhl7string); string value = hl7toxmlconverter. gettext (xmlobject, "QRD/qrd.9/qrd.9.0", 0); string value2 = hl7toxmlconverter. gettext (xmlobject, "QRD/qrd.9/qrd.9.0"); string nodevalue = xmlobject. documentelement. selectsinglenode ("MSH/msh.1 "). innertext; string nodevalue2 = xmlobject. documentelement. selectsinglenode ("QRD/qrd.9/qrd.9.0 "). innertext;