简体   繁体   中英

In Mirth How to parse xml string

I have one column output from DB as a xml string. I want to parse this xml objects I am able to convert this as xml string by using

var xmlResponse=new XML(msg['xmlcontent'].toString());

in the next line i want loop through each node how to do this if there is any alternate method please suggest below is the xml string using mirth.

<?xml version="1.0" encoding="utf-8" ?><ORDERMESSAGE><MSH><Sending_Application>HIS</Sending_Application><Sending_Facility>555</Sending_Facility><Receiving_Application>LIS</Receiving_Application><Receiving_Facility/><Message_Date_Time>20160803122348</Message_Date_Time><Security/><Message_Type>ORM</Message_Type><Message_Control_Id/></MSH><ENV><Event_Type_Code>O01</Event_Type_Code><Recorded_DateTime>20160803122348</Recorded_DateTime></ENV><PID><Patient_Id_Ext/><Patient_Id_Int>448</Patient_Id_Int><Patient_Id_Type/><Patient_Id_Number/><Patient_Name>Test Health Check</Patient_Name><Date_Of_Birth>19810802</Date_Of_Birth><Gender_Code>F</Gender_Code><Race_code/><Address1>GUINDY</Address1><Address2/><Address3/><City>Bid</City><State>Maharashtra</State><Postal_Code/><Country_Code>India</Country_Code><Phone_Home/><Phone_Mobile>9764646545</Phone_Mobile><Phone_Business/><Primary_Language/><Marital_Status_Code>Married</Marital_Status_Code><Nationality_Code>Indian</Nationality_Code></PID><PV1><SetIDPatientVisit>574</SetIDPatientVisit><Patient_Type_Code>O</Patient_Type_Code><Ward_Code/><Admission_Type/><Preadmit_Number/><Specialty_Code/><Referring_Doctor_Code/><Referring_Doctor_Desc/><Consulting_Doctor_Code/><Consulting_Doctor_Desc/><Admitting_Doctor_Code/><Admitting_Doctor_Desc/><Patient_Class/><Visit_Number>1146</Visit_Number><Financial_Class_Code/><Admit_DateTime>20160803122003</Admit_DateTime></PV1><ORC><Order_Control>NW</Order_Control><ORC_Placer_Order_Number>544</ORC_Placer_Order_Number><ORC_Filler_Order_Number/><Placer_Group_Number/><Order_Status_Code>Paid</Order_Status_Code><Response_Flag/><ORC_Quantity_Timing>20160803122032</ORC_Quantity_Timing><Parent/><DateTime_Of_Transaction>20160803122032</DateTime_Of_Transaction><Entered_By_ID>9188</Entered_By_ID><Entered_By_Name>Kutti</Entered_By_Name><Verified_By_ID/><Verified_By_Name/><ORC_Ordering_Provider_ID>1</ORC_Ordering_Provider_ID><ORC_Ordering_Provider_Name/><Enter_Location/><Call_Back_Phone_Number/><Order_Effective_DateTime/><Order_Control_Code_Reason/><Entering_Organization/><Entering_Device/><Action_By/><Advanced_Beneficiary_Notice_Code/><Ordering_Facility_Code/><Ordering_Facility_Name/></ORC><OBR><Set_Id_OBR>1</Set_Id_OBR><OBR_Placer_Order_Number>309</OBR_Placer_Order_Number><OBR_Filler_Order_Number/><Package_Code/><Package_Description/><Test_Code/><Test_Name>ABSOLUTE BLAST COUNT</Test_Name><Priority_Code/><Priority_Desc/><Requested_DateTime>20160803122032</Requested_DateTime><Observation_DateTime/><Observation_End_DateTime/><Collection_Volume/><Collector_Identifier/><Specimen_Action_Code/><Danger_Code/><Relevant_Clinical_Info/><Specimen_Received_DateTime/><Specimen_Source_Code/><Specimen_Source_Desc/><OBR_Ordering_Provider_ID>1</OBR_Ordering_Provider_ID><OBR_Ordering_Provider_Name/><Order_Callback_Phone_Number/><Placer_Field1/><Placer_Field2/><Filler_Field1/><Filler_Field2/><Status_Change_DateTime/><Charge_To_Practice/><Diagnostic_Serv_Sect_Code/><Diagnostic_Serv_Sect_Desc/><Result_Status/><Parent_Result/><OBR_Quantity_Timing/><Unit_Code/><Unit_Desc/><Result_Copies_To/><Transportation_Mode/><Reason_For_Study/><Principal_Result_Interpreter/><Assistant_Result_Interpreter/><Technician/><Transcriptionist/><Scheduled_DateTime/><Number_Of_Sample_Containers/><Transport_Logistics_Of_Collected_Sample/><Collector_Comment/><Transport_Arrangement_Responsibility/><Transport_Arranged/><Escort_Required/><Planned_Patient_Transport_Comment/><Procedure_Code/><Procedure_Code_Modifier/><Placer_Supplemental_Service/><Filler_Supplemental_Service/><Cancel_Reason_Code/><Cancel_Reason_Desc/><FeeId>23</FeeId><FeeType>INV</FeeType><MappingId>0</MappingId></OBR></ORDERMESSAGE>

Mirth Connect uses Rhino as a JavaScript engine. In its turn, JavaScript uses E4X or ECMAScript for XML to simplify the task of writing code for dealing with XML.

So basically your question has two parts:

1) to access or assign individual fields in your data feed you can use dot notation, like this:

var eventType = xmlResponse['ENV']['Event_Type_Code'].toString();

2) to iterate through all or repeating nodes check for Mirth reference functions by selecting "Message Functions" category (on the top right side of any transformer editor), here you will see "Iterate Over Segment" or "Iterate Over All Segments" code snippets, then drag and modify it, like this:

for each (seg in xmlResponse.OBR.children()) {
    if (seg.name().toString() == "SEG") {
        var sample_value = seg['SEG.1']['SEG.1.1'].toString();
    }
}

But in your particular case you don't have repeating segments as far as I can tell, so you should be good to go with the first option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM