Announcement

Collapse
No announcement yet.

Building an XML File

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Building an XML File

    Hi,

    I have a new channel that I am creating to query a SQL dB and send an XML File. Currently, I have it working where it sends 1 XML file for every record returned from the SQL query. I was wondering how I would go about modifying the channel to create 1 XML file that contained every record returned from the resultset.

    This is my current setup:
    Source: SQL connection that executes a proc and returns the needed values

    Source Outbound Message Template:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <notes>
      <note>
        <nemc_hospital_number>2226362</nemc_hospital_number>
        <last_name>LAM</last_name>
        <first_name>HOWARD</first_name>
        <birth_date>1/2/1984</birth_date>
        <patient_id>252845</patient_id>
        <progress_note_id>820000781203</progress_note_id>
        <activity_code>A</activity_code>
        <note_date>3/21/2011</note_date>
        <written_by>Elizabeth Parke</written_by>
        <note_subject>Nutrition Note</note_subject>
        <progress_note_text>Assessment:</progress_note_text>
      </note>
    </notes>
    Source Mappings:
    Code:
    tmp['note']['nemc_hospital_number'] = msg['nemc_hospital_number'].toString()
    tmp['note']['last_name'] =msg['last_name'].toString()
    etc.....
    Destination: File Writer
    Destination Template: ${message.encodedData}

    Currently, my sent message values are mapped correctly and the XML file that is created matches the format of my Outbound Template, but only contains 1 record. I am assuming I need to loop through and manually create the file, but I have little experience with XML and have only worked on HL7 channels in Mirth before. Any help or tips would be appreciated

  • #2
    Well, I seem to have figured it out. I just created a transformer step that looped through the result_set and built the message manually. I created a channelMap variable for the final message and used this in my Destination Template. Everything seems to work fine now, aside from an 'expected a semicolon' error, which I suspect has to do with text of once of the records I am returning. Anyway, Ill post my code below for future use by anyone that has to do a similar channel:
    Code:
    var dbConn = DatabaseConnectionFactory.createDatabaseConnection(
    	'net.sourceforge.jtds.jdbc.Driver',
    	'jdbc:jtds:sqlserver://server:port/instance',
    	'user',
    	'pass'
    );
    
     var sql = "SELECT nemc_hospital_number,"+
      			"last_name,"+
     			"first_name,"+
     			"birth_date,"+
      			"patient_id,"+
      			"progress_note_id,"+
      			"activity_code,"+
      			"note_date,"+
      			"written_by,"+
      			"note_subject,"+
      			"progress_note_text"+
     			" from progress_note_nemc_extract where updated='N'"
    
    
    var results = dbConn.executeCachedQuery(sql);
    
    var csv = "";
    var csv2 = "";
    var row = "";
    
    while(results.next()) {
    
    	row = '<note>' + '\n' +
    		  '<nemc_hospital_number>' + results.getString('nemc_hospital_number') + '</nemc_hospital_number>' + '\n' +
    		  '<last_name>' + results.getString('last_name') + '</last_name>' + '\n' +
      		  '<first_name>' + results.getString('first_name') + '</first_name>' + '\n' +
        	  '<birth_date>' + results.getString('birth_date') + '</birth_date>' + '\n' +
        	  '<patient_id>' + results.getString('patient_id') + '</patient_id>' + '\n' +
        	  '<progress_note_id>' + results.getString('progress_note_id') + '</progress_note_id>' + '\n' +
        	  '<activity_code>' + results.getString('activity_code') + '</activity_code>' + '\n' +
        	  '<note_date>' + results.getString('note_date') + '</note_date>' + '\n' +
        	  '<written_by>' + results.getString('written_by') + '</written_by>' + '\n' +
        	  '<note_subject>' + results.getString('note_subject') + '</note_subject>' + '\n' +
        	  '<progress_note_text>' + results.getString('progress_note_text') + '</progress_note_text>' + '\n' +
    		  '</note>';
    	 csv = csv + row;
         csv2 = '<?xml version="1.0" encoding="utf-8"?>' + '\n' + '<notes>' + csv + '</notes>';
    }
    channelMap.put('csv2', csv2);
    
    // mark all patient entries as exported
    sql = "update progress_note_nemc_extract"+
    	  " set updated='Y'"+";";
    		dbConn.executeUpdate(sql);
    dbConn.close();

    Comment

    Working...
    X