Search code examples
javascriptxmlmirth

iterating and extracting data from xml in javascript on mirth


I'm using mirth connect 3.7, java version 1.8. i am new to both mirth and javascript. I have set up a channel destination to a javascript writer to get data out of xml files inserted into a mysql db. a sample section of xml file as follows: ...

   <DG1>
        <DG1.1>
            <DG1.1.1>1</DG1.1.1>
        </DG1.1>
        <DG1.2>
            <DG1.2.1>I10</DG1.2.1>
        </DG1.2>
        <DG1.3>
            <DG1.3.1>R10.9</DG1.3.1>
        </DG1.3>
        <DG1.4>
            <DG1.4.1>UNSPECIFIED ABDOMINAL PAIN</DG1.4.1>
        </DG1.4>
        <DG1.5/>
        <DG1.6>
            <DG1.6.1>A</DG1.6.1>
        </DG1.6>
        <DG1.7/>
        <DG1.8>
            <DG1.8.1>391</DG1.8.1>
        </DG1.8>
        <DG1.9/>
        <DG1.10/>
        <DG1.11>
            <DG1.11.1>4252.21</DG1.11.1>
        </DG1.11>
        <DG1.12/>
        <DG1.13/>
        <DG1.14/>
        <DG1.15/>
        <DG1.16/>
        <DG1.17/>
        <DG1.18>
            <DG1.18.1>N</DG1.18.1>
        </DG1.18>
    </DG1>
    <DG1>
        <DG1.1>
            <DG1.1.1>2</DG1.1.1>
        </DG1.1>
        <DG1.2>
            <DG1.2.1>I10</DG1.2.1>
        </DG1.2>
        <DG1.3>
            <DG1.3.1>R10.9</DG1.3.1>
        </DG1.3>
        <DG1.4>
            <DG1.4.1>UNSPECIFIED ABDOMINAL PAIN</DG1.4.1>
        </DG1.4>
        <DG1.5/>
        <DG1.6>
            <DG1.6.1>A</DG1.6.1>
        </DG1.6>
        <DG1.7/>
        <DG1.8>
            <DG1.8.1>391</DG1.8.1>
        </DG1.8>
        <DG1.9/>
        <DG1.10/>
        <DG1.11>
            <DG1.11.1>4252.21</DG1.11.1>
        </DG1.11>
        <DG1.12/>
        <DG1.13/>
        <DG1.14/>
        <DG1.15/>
        <DG1.16/>
        <DG1.17/>
        <DG1.18>
            <DG1.18.1>N</DG1.18.1>
        </DG1.18>
    </DG1>

...

I am trying to get the datapoints out of the xml iteratively so i can insert these diagnosis codes in a mysql table. my script at this point:

try {
    var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('com.mysql.jdbc.Driver', 'jdbc:mysql://127.0.0.1:3306/adt', 'xxx', 'xxx');
    var xml = new XML(connectorMessage.getEncodedData());
    var myNodeList = xml.querySelectorAll("DG1");

    for (i = 0; i < myNodelist.length; i++) {
            var myQuery = 'INSERT INTO adt.diagnosis (AcctNum, MRN, ICD10) VALUES ("'+ $('AcctNum') + '", "' + $('MedRecNum') + '", "' +  myNodelist[i]['DG1.3']['DG1.3.1'] +  '")';
            //do something with myVar to get a query...
            dbConn.executeUpdate(myQuery);
    }

} catch (ex) {
    //handle any exceptions...
}

it runs without exceptions but i am not capturing the intended data obviously. Again, new to javascript, mirth and parsing xml. Questions:

  1. obviously, i'm referencing the data points inappropriately, what is the nomenclature in javascript?
  2. is there a dev environment in mirth that i can step through code and better troubleshoot?
  3. are there any good recommended resources for javascript and xml as it pertains to mirth?

Solution

  • Mirth uses Mozilla Rhino for its Javascript engine. Rhino uses a deprecated standard called e4x for XML processing. If you search Google for e4x you'll find several pages at developer.mozilla.org with scary "obsolete" banners everywhere that can be helpful. The mirth user guide is very detailed when it comes to workflow within mirth.

    https://github.com/mozilla/rhino

    https://web.archive.org/web/20181120184304/https://wso2.com/project/mashup/0.2/docs/e4xquickstart.html (another good e4x resource)

    https://www.nextgen.com/products-and-services/NextGen-Connect-Integration-Engine-Downloads (for the user guide)

    I'm surprised querySelectorAll wasn't throwing an error. With minimal changes to your code:

    try {
        var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('com.mysql.jdbc.Driver', 'jdbc:mysql://127.0.0.1:3306/adt', 'xxx', 'xxx');
        var xml = new XML(connectorMessage.getEncodedData());
        // This should work, but is not typically how hl7 segments are accessed. Would need to see more than a segment for typical usage.
        var myNodeList = xml.descendants("DG1"); // returns type XMLList
    
        // length is a function instead of property on XMLList objects
        for (i = 0; i < myNodelist.length(); i++) {
                var myQuery = 'INSERT INTO adt.diagnosis (AcctNum, MRN, ICD10) VALUES ("'+ $('AcctNum') + '", "' + $('MedRecNum') + '", "' +  myNodelist[i]['DG1.3']['DG1.3.1'] +  '")';
                dbConn.executeUpdate(myQuery);
        }
    } catch (ex) {
        //handle any exceptions...
    }
    

    Using a for each loop and parameterized sql statement:

    try {
        var dbConn = DatabaseConnectionFactory.createDatabaseConnection ('com.mysql.jdbc.Driver', 'jdbc:mysql://127.0.0.1:3306/adt', 'xxx', 'xxx');
        var xml = new XML(connectorMessage.getEncodedData());    
        var myQuery = 'INSERT INTO adt.diagnosis (AcctNum, MRN, ICD10) VALUES (?, ?, ?)';
    
        for each (var dg1 in xml.descendants('DG1')) {
                dbConn.executeUpdate(myQuery, new java.util.ArrayList([$('AcctNum'), $('MedRecNum'), dg1['DG1.3']['DG1.3.1'].toString()]));
        }
    } catch (ex) {
        //handle any exceptions...
    }
    

    You'll want a finally block after your try to close your database connection. If you remove the catch block, mirth will automatically set the message status to ERROR, write the exception to the server log, and fire an event which you can act on with a defined alert. That's usually easier than trying to handle the exception yourself.