web stats
Building an XML File - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 03-22-2011, 10:53 AM
upstart33 upstart33 is offline
Mirth Guru
 
Join Date: Dec 2010
Location: Chicago, IL.
Posts: 461
upstart33 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 03-22-2011, 12:44 PM
upstart33 upstart33 is offline
Mirth Guru
 
Join Date: Dec 2010
Location: Chicago, IL.
Posts: 461
upstart33 is on a distinguished road
Default

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();
Reply With Quote
Reply

Tags
mapping, template, xml

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 11:12 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation