No announcement yet.

Building an XML File

  • Filter
  • Time
  • Show
Clear All
new posts

  • Building an XML File


    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:
    <?xml version="1.0" encoding="utf-8"?>
        <written_by>Elizabeth Parke</written_by>
        <note_subject>Nutrition Note</note_subject>
    Source Mappings:
    tmp['note']['nemc_hospital_number'] = msg['nemc_hospital_number'].toString()
    tmp['note']['last_name'] =msg['last_name'].toString()
    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:
    var dbConn = DatabaseConnectionFactory.createDatabaseConnection(
     var sql = "SELECT nemc_hospital_number,"+
     			" from progress_note_nemc_extract where updated='N'"
    var results = dbConn.executeCachedQuery(sql);
    var csv = "";
    var csv2 = "";
    var row = "";
    while( {
    	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' +
    	 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'"+";";