如何使用 Mirth 将 xml 文件中的数据插入 SQL 服务器数据库

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

我想使用 Mirth 从 XML 文件 (CCD) 中获取数据并将其放入我的 SQL 服务器数据库中。

所以我在我的电脑上安装了 Mirth connect administrator,然后我刚刚创建了一个带有源 XML 文件的新频道,以及我的 SQL 服务器数据库的目标。我还选择了我的数据库的 table,Mirth 自动创建了这个查询:

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

现在的问题是,我的 CCD 文档(文件 .xml)的部分(我想插入到我的数据库中)具有以下结构:

<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>

如你所见,有两个标签

entry typeCode="DRIV"

我想在我的数据库中插入与条目标记一样多的记录。 在这个例子中,我应该在我的数据库中插入 2 条记录。

您可以在 Java脚本编写器中执行类似以下操作。我更喜欢 JavaScript writer 而不是 Database Writer,因为你有更多的灵活性并且可以从 JavaScript 调用所有相同的本地 Mirth Java。

如果您将 XML(从文件 Reader,如果我理解正确...)从源传递到目标,请将目标类型设置为 Java脚本编写器,然后像这样遍历你的对象:

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...
}

XML 的迭代是使用 E4X 完成的:https://developer.mozilla.org/en-US/docs/Archive/Web/E4X_tutorial