sql-serverxmlmirthccdhl7-v3

How to use Mirth to insert data in SQL Server database from xml file


I want to use Mirth to get data from XML file (CCD) and put its, in my SQL Server database.

So I have install Mirth connect administrator on my pc, then I have just created a new Channel with Source XML file, and to Destination my SQL server database. I have also selected table of my database, and automatically, Mirth have created this query:

INSERT INTO CLINICAL_DOC_Problems (Id, IdRoot, IdExtension, IdObservationRoot, IdObservationExtension, EffectiveTime, EffectiveTimeMin, EffectivTimeMax, CodeSystem, Code, CodeSystemStatus, CodeStatus, IdSection)
VALUES (, , , , , , , , , , , , )

Now the problem is this, the section (that I want insert in my database) of my CCD document (file .xml) have this structures:

<component>
<section>
    <templateId root='2.16.840.1.113883.10.20.1.11'/> <!-- Problem section template -->
    <code code="11450-4" codeSystem="2.16.840.1.113883.6.1"/> 
    <entry typeCode="DRIV">
        <act classCode="ACT" moodCode="EVN">
            <templateId root='2.16.840.1.113883.10.20.1.27'/> <!-- Problem act template -->
            <id root="6a2fa88d-4174-4909-aece-db44b60a3abb"/>
            <entryRelationship typeCode="SUBJ">
                <observation classCode="OBS" moodCode="EVN">
                    <templateId root='2.16.840.1.113883.10.20.1.28'/> <!-- Problem observation template -->
                    <id root="d11275e7-67ae-11db-bd13-0800200c9a66"/>
                    <code code="ASSERTION" codeSystem="2.16.840.1.113883.5.4"/>                 
                    <effectiveTime><low value="1950"/></effectiveTime>
                    <value xsi:type="CD" code="195967001" codeSystem="2.16.840.1.113883.6.96" displayName="Asthma"/>
                    <entryRelationship typeCode="REFR">
                        <observation classCode="OBS" moodCode="EVN">
                            <templateId root='2.16.840.1.113883.10.20.1.50'/> <!-- Problem status observation template -->
                            <code code="33999-4" codeSystem="2.16.840.1.113883.6.1" displayName="Status"/>
                            <value xsi:type="CE" code="55561003" codeSystem="2.16.840.1.113883.6.96" displayName="Active"/>
                        </observation>
                    </entryRelationship>
                </observation>
            </entryRelationship>
        </act>  
    </entry>
    <entry typeCode="DRIV">
        <act classCode="ACT" moodCode="EVN">
            <templateId root='2.16.840.1.113883.10.20.1.27'/> <!-- Problem act template -->
            <id root="ec8a6ff8-ed4b-4f7e-82c3-e98e58b45de7"/>
            <entryRelationship typeCode="SUBJ">
                <observation classCode="OBS" moodCode="EVN">
                    <templateId root='2.16.840.1.113883.10.20.1.28'/> <!-- Problem observation template -->
                    <id root="ab1791b0-5c71-11db-b0de-0800200c9a66"/>
                    <code code="ASSERTION" codeSystem="2.16.840.1.113883.5.4"/>
                    <value xsi:type="CD" code="233604007" codeSystem="2.16.840.1.113883.6.96" displayName="Pneumonia"/>
                    <entryRelationship typeCode="REFR">
                        <observation classCode="OBS" moodCode="EVN">
                            <templateId root='2.16.840.1.113883.10.20.1.50'/> <!-- Problem status observation template -->
                            <code code="33999-4" codeSystem="2.16.840.1.113883.6.1" displayName="Status"/>
                            <value xsi:type="CE" code="413322009" codeSystem="2.16.840.1.113883.6.96" displayName="Resolved"/>
                        </observation>
                    </entryRelationship>
                </observation>
            </entryRelationship>
        </act>
    </entry>
</section>
</component>

As you can see, there are two tag

entry typeCode="DRIV"

I want to insert in my database as many records as entry tag. In this example, I should to insert in my database 2 records.


Solution

  • You could do something like the following in a JavaScript writer. I prefer the JavaScript writer over the Database Writer because you've got a lot more flexibility and can call all of the same native Mirth Java from the JavaScript.

    If you pass your XML (from a File Reader, if I understand correctly...) from your source to your destination, set the destination type as a JavaScript writer, and then iterate through your objects like so:

    var dbConn;
    try {
        dbConn = DatabaseConnectionFactory.createConnection(driver, address, username, password);
        var xml = new XML(connectorMessage.getEncodedData());
        for(var i = 0; i < xml.section.entry.length(); i++) {
            if(xml.section.entry[i].@typeCode == 'DRIV') {
                var myData = xml.section.entry[i].act;
                var myQuery = '';
                //do something with myVar to get a query...
                dbConn.executeCachedQuery(myQuery);
            }
        }
    } catch (ex) {
        //handle any exceptions...
    }
    

    Iteration through XML is done using E4X: https://developer.mozilla.org/en-US/docs/Archive/Web/E4X_tutorial