web stats
XML transformer - Mirth Community

Go Back   Mirth Community > Mirth Connect > Development

Thread Tools Display Modes
Old 05-16-2019, 01:52 AM
Adrien Adrien is offline
Mirth Newb
Join Date: Mar 2019
Posts: 9
Adrien is on a distinguished road
Default XML transformer

I created a channel.

In this source channel (file reader), I get back my XML file which is in a folder.
Here is a part of the file :


With a transformer I would like to add this :

<prestataire><medecins><medecin><numeroAdeli>12345 </numeroAdeli>

between the tag : </execute> and <NABMs>

In Destination transformer I create step : type -> Javascript
I dont know how to code what I want. ( I never develop on mirth and
I know a little about JavaScript)

I need help coding my task!

Thank you in advance to any one who may be able to give me some ideas.

Reply With Quote
Old 05-22-2019, 09:12 AM
collinsmj collinsmj is offline
OBX.1 Kenobi
Join Date: Jun 2014
Location: Southern USA
Posts: 27
collinsmj is on a distinguished road

I would be happy to assist you in your problem. There are many ways to do what you want. I couldn't possible cover all of them. I will past the code from my message transformer that results in your desired message.

I had to immediately edit my reply to include the following:
I am using Mirth Connect 3.5.2.
The notation used to alter the msg body in Mirth Connect is E4X. E4X is a rather old javascript library. This code will not run in a regular javascript engine. Knowing only a little javascript isn't an issue. A larger issue is you are new to E4X and it's syntax. But this will help you. It takes a bit of time to learn the odd bits and catches.
#end note

I started with an input XML message of

    <NABMs>stuff here</NABMs>
All xml messages have a root node. You didn't post what your message's root node was in your example so I am assuming <root>.

We can now transform the message in a number of ways.

This will add a blank node after the <execute>...</execute> block. please note I use the += operator. This would be equivalent to msg[execute] = msg[execute] + new XML(...);
  msg['execute'] += new XML("<prestataire/>");
This code defines your raw XML and assigns it to a variable. Which is then used to replace an xmlnode. ValueToBeReplaced will not appear in the input or output xml. This is just to prove a point of understanding how the assignment works.
var a = <medecins>
msg['prestataire']['ValueToBeReplaced'] = a;

The following will declare a new XML variable and assign values to child nodes. The child nodes are created during runtime when the values are assigned to them. Then we assign the entire XML node to a location in the message.
var b = new XML('<identification></identification>');
b['code'] = 'AAA';
b['libelle'] = 'BBB';

msg['prestataire']['medecins']['medecin']['numeroAdeli'] += b;
Or we could just ignore the XML brackets and formating rules. We just create the entire structure with these two assignment statements. UniteFonctionnelle, code, or libelle do not exist until the child values are assigned.
msg['prestataire']['uniteFonctionnelle']['code'] = 789;
msg['prestataire']['uniteFonctionnelle']['libelle'] = 'YYY';

The result is the following.

    <NABMs>stuff here</NABMs>

Take the code blocks one by one and add them to your transformer. Run a test each time and you will see how each block modifies the message.

Happy coding!

Last edited by collinsmj; 05-22-2019 at 09:18 AM. Reason: added a note
Reply With Quote

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 08:11 PM.

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