HL7 2.x parsing (Java version)

Source: Internet
Author: User

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 years of hospital informatization, HL7 Standard Organization and resolution are the most complicated. The following is my use of HL7 engine Analysis for many years. There are two major versions: 1.c# and 2. java.

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. export enthelper; 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 sh L7) {document = converttoxmlobject (shl7); string hl7str = document. asxml (); Return hl7str;} public static string converttoxml (document) {string hl7str = document. asxml (); Return hl7str;} public static document converttoxmlobject (string shl7) {document = createxmldoc (); // split HL7 into 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 ++) {// determines whether there are empty rows if (shl7lines [I]! = NULL) {string shl7line = shl7lines [I]; // string [] sfields = getmessgefields (shl7line) are separated by/R or/N carriage returns; // segment (one line) create Level 1 node element El = document. getrootelement (). addelement (sfields [0]); // loop each row Boolean ismsh = true; For (int A = 1; A <sfields. length; A ++) {// whether the connection string of HL7 is included ^ ~ \ & If (sfields [A]. indexof ('^')> 0 | sfields [A]. indexof ('~ ')> 0 | sfields [A]. indexof ('\')> 0 | sfields [A]. indexof ('&')> 0) {// 0: if this line has any separator, // start the operation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ // Pass ~ Separate string [] scomponents = getrepetitions (sfields [a]); If (scomponents. length> 1) {// 1: If you can separate 0001 ^ Guo Jing ^ health check number ^ EQ ^ and ~ 0002 ^ Zone 1 ^ EQ ^ and for (INT B = 0; B <scomponents. length; B ++) {// element fieldel1 = el. addelement (sfields [0] + ". "+ a); createcomponents (El, scomponents [B], sfields [0], a, B) ;}} else {// 1: if there is only one 0001 ^ Guo Jing ^ health check number ^ EQ ^ and // create level 2 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 separator // create the second-level node element fieldel = El for the field. addelement (sfields [0] + ". "+ a); fieldel. settext (sfields [a]) ;}}// end if} // end for // modify the values 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, int B) {element componentel = el. addelement (sfield + ". "+ a); // element componentel = El ;//. addelement (sfield + ". "+ A + ". "+ B ); //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& & // use & to separate string [] subcomp Onents = getsubcomponents (hl7components); If (subcomponents. length> 1) {// 2. If there is a word group, it is generally not... Use & rarely use} else {// 2. if no, use ^ grouping string [] srepetitions = getcomponents (hl7components); If (srepetitions. length> 1) {element repetitionel = NULL; For (int c = 0; C <srepetitions. length; C ++) {repetitionel = componentel. addelement (sfield + ". "+ A + ". "+ (C + 1); repetitionel. settext (srepetitions [c]) ;}} else {componentel. settext (hl7components );}} //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&& & Return El ;} /// <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) {return S. split ("\ ^") ;}/// <summary> /// use & to separate sub-group fields /// </su Mmary> /// <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 document createxmldoc () {Document Output = incluenthelper. createdocument (); // generate a contact element rootnode = output. addelement ("hl7message"); Return output;} public static string gettext (document, string path) {node = document. selectsinglenode ("hl7message/" + path); If (node! = NULL) {return node. gettext () ;}else {return NULL ;}} public static string gettext (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, string path) {list nodes = document. selectnodes ("hl7message/" + path); Return nodes;} public static void writedocument (document, string filepath) {try {// Read File // filewriter = new filewriter (filepath); writer = new outputstreamwriter (New fileoutputstream (filepath), "UTF-8 "); // set the file encoding outputformat xmlformat = new outputformat (); xmlformat. setencoding ("UTF-8"); // method for creating a Write File xmlwriter = new xmlwriter (writer, xmlformat); // xmlwriter for writing files. 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 ^ health Check number ^ EQ ^ and ~ 0002 ^ region 1 ^ EQ ^ and \ nqrf | 20110627 | 20110803 "; document = hl7toxmlconverter. converttoxmlobject (myhl7string); // obtain the 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: List) {system. out. println (":" + node. gettext ();} system. out. println (hl7toxmlconverter. converttoxml (myhl7string ));

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.