web stats
XML transformer - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-16-2019, 02:04 AM
Adrien Adrien is offline
Mirth Newb
 
Join Date: Mar 2019
Posts: 7
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 :

<execute>
<date>2019-03-05</date>
<heure>11:33:00</heure>
</execute>
<NABMs>...</NABMs>

With a transformer I would like to add this :

<prestataire><medecins><medecin><numeroAdeli>12345 </numeroAdeli>
<identification><code>AAA</code><libelle>BBB</libelle></identification>
</medecin></medecins>
<uniteFonctionnelle><code>789</code><libelle>YYY</libelle>
</uniteFonctionnelle>
</prestataire>

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.

Adrien
Reply With Quote
  #2  
Old 05-16-2019, 06:04 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,192
cory_cole is on a distinguished road
Default

Assuming this is the highest level tags. i.e.

<document>
<execute>
<date>2019-03-05</date>
<heure>11:33:00</heure>
</execute>
<NABMs>...</NABMs>
</document>

use

document.insertAdjacentHTML('execute', the text you want added)
Reply With Quote
  #3  
Old 05-17-2019, 12:41 AM
mdehoog mdehoog is offline
Mirth Guru
 
Join Date: Mar 2011
Location: Apeldoorn the Netherlands
Posts: 277
mdehoog is on a distinguished road
Send a message via Skype™ to mdehoog
Default XML insert

An other option.
Code:
createSegmentAfter('prestataire', msg['execute']);
msg['prestataire']['medecins']['medecin']['numeroAdeli'] = '1234567890';
Input:
Code:
<document>
    <execute>
        <date>2019-03-05</date>
        <heure>11:33:00</heure>
    </execute>
    <NABMs/>
</document>
Output:
Code:
<document>
    <execute>
        <date>2019-03-05</date>
        <heure>11:33:00</heure>
    </execute>
    <prestataire>
        <medecins>
            <medecin>
                <numeroAdeli>1234567890</numeroAdeli>
            </medecin>
        </medecins>
    </prestataire>
    <medecins/>
    <NABMs/>
</document>
See the channel included.

Success
Attached Files
File Type: xml XML insert.xml (10.6 KB, 1 views)
__________________
X Connections
http://www.x-connections.com
Reply With Quote
  #4  
Old 05-17-2019, 05:54 AM
Adrien Adrien is offline
Mirth Newb
 
Join Date: Mar 2019
Posts: 7
Adrien is on a distinguished road
Default

I tried createSegmentAfter here is the error :

Transformer error
ERROR MESSAGE: Error evaluating transformer
com.mirth.connect.server.MirthJavascriptTransforme rException:
CHANNEL: XML ajout
CONNECTOR: Destination 1
SCRIPT SOURCE: TRANSFORMER
SOURCE CODE:
56: }
57: }
58: return result;
59: }
60: function createSegment(name, msgObj, index) {if (arguments.length == 1) { return new XML('<' + name + '></' + name + '>'); };if (arguments.length == 2) { index = 0; };msgObj[name][index] = new XML('<' + name + '></' + name + '>');return msgObj[name][index];}
61: function createSegmentAfter(name, segment) {var msgObj = segment;while (msgObj.parent() != undefined) { msgObj = msgObj.parent(); }msgObj.insertChildAfter(segment[0], new XML('<' + name + '></' + name + '>'));return msgObj.child(segment[0].childIndex() + 1);}
62: function getArrayOrXmlLength(obj) {
63: if (typeof obj == 'xml' || obj instanceof java.lang.String) {
64: return obj.length();
65: } else if (typeof obj != 'undefined' && obj != null) {
LINE NUMBER: 61
DETAILS: TypeError: insertChildAfter n'est pas une fonction, c'est un xml
at 2f6082c4-df30-4eb1-8342-977e857d7579:61 (createSegmentAfter)
at 2f6082c4-df30-4eb1-8342-977e857d7579:104 (doTransform)
at 2f6082c4-df30-4eb1-8342-977e857d7579:127 (doScript)
at 2f6082c4-df30-4eb1-8342-977e857d7579:129
at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:154)
at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:119)
at com.mirth.connect.server.util.javascript.JavaScrip tTask.call(JavaScriptTask.java:113)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
at java.lang.Thread.run(Unknown Source)
Reply With Quote
  #5  
Old 05-17-2019, 01:58 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 718
agermano is on a distinguished road
Default

Check that your inbound message has 1 and only 1 occurrence of msg['execute']. You'll get that message if you pass an empty list or a list with more than one node to createSegmentAfter. If there are multiple occurrences, you'll have to specify which one you want it to come after.
Reply With Quote
  #6  
Old 05-19-2019, 11:20 PM
Adrien Adrien is offline
Mirth Newb
 
Join Date: Mar 2019
Posts: 7
Adrien is on a distinguished road
Default

Ok i understand!

In my inbound message i have 3 times of msg['execute'].

I would like to integrate it at the first occurrence.

How can I do that?
Reply With Quote
  #7  
Old 05-20-2019, 07:12 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 718
agermano is on a distinguished road
Default

To always insert after the first occurrence, change
Code:
createSegmentAfter('prestataire', msg['execute']);
to
Code:
createSegmentAfter('prestataire', msg['execute'][0]);
Reply With Quote
  #8  
Old 05-20-2019, 11:33 PM
Adrien Adrien is offline
Mirth Newb
 
Join Date: Mar 2019
Posts: 7
Adrien is on a distinguished road
Default

code :
createSegmentAfter('prestataire', msg['execute'][0]);
msg ['prestataire']['medecins']['medecin']['numeroAdeli'] = '1234567890';

Error : Transformer error
ERROR MESSAGE: Error evaluating transformer
com.mirth.connect.server.MirthJavascriptTransforme rException:
CHANNEL: XML ajout
CONNECTOR: Destination 1
SCRIPT SOURCE: TRANSFORMER
SOURCE CODE:
56: }
57: }
58: return result;
59: }
60: function createSegment(name, msgObj, index) {if (arguments.length == 1) { return new XML('<' + name + '></' + name + '>'); };if (arguments.length == 2) { index = 0; };msgObj[name][index] = new XML('<' + name + '></' + name + '>');return msgObj[name][index];}
61: function createSegmentAfter(name, segment) {var msgObj = segment;while (msgObj.parent() != undefined) { msgObj = msgObj.parent(); }msgObj.insertChildAfter(segment[0], new XML('<' + name + '></' + name + '>'));return msgObj.child(segment[0].childIndex() + 1);}
62: function getArrayOrXmlLength(obj) {
63: if (typeof obj == 'xml' || obj instanceof java.lang.String) {
64: return obj.length();
65: } else if (typeof obj != 'undefined' && obj != null) {
LINE NUMBER: 61
DETAILS: TypeError: Cannot call method "{1}" of {0}
at 6deebaab-a76f-4795-ae65-8752b5144d6c:61 (createSegmentAfter)
at 6deebaab-a76f-4795-ae65-8752b5144d6c:104 (doTransform)
at 6deebaab-a76f-4795-ae65-8752b5144d6c:127 (doScript)
at 6deebaab-a76f-4795-ae65-8752b5144d6c:129
at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:154)
at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:119)
at com.mirth.connect.server.util.javascript.JavaScrip tTask.call(JavaScriptTask.java:113)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
at java.lang.Thread.run(Unknown Source)

I don't understand why it doesn't work!
Reply With Quote
  #9  
Old 05-21-2019, 07:23 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 718
agermano is on a distinguished road
Default

I'm not sure how to reproduce that error message. Can you provide a full sample message that isn't working?
Reply With Quote
Reply

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 06:16 AM.


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