Announcement

Collapse
No announcement yet.

XML transformer

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

  • 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

  • #2
    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)

    Comment


    • #3
      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
      X Connections
      https://documentor.email
      https://www.x-connections.com

      Comment


      • #4
        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)

        Comment


        • #5
          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.

          Comment


          • #6
            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?

            Comment


            • #7
              To always insert after the first occurrence, change
              Code:
              createSegmentAfter('prestataire', msg['execute']);
              to
              Code:
              createSegmentAfter('prestataire', msg['execute'][0]);

              Comment


              • #8
                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!

                Comment


                • #9
                  I'm not sure how to reproduce that error message. Can you provide a full sample message that isn't working?

                  Comment


                  • #10
                    a file that does not work for me
                    Attached Files

                    Comment


                    • #11
                      It's having an issue because that particular message does not have an 'execute' child element. What do you need to do in that case?

                      Comment


                      • #12
                        In this case, I need to add 'prestataire' between the </ execute> and <NABMs> tags.

                        Comment


                        • #13
                          The error is because msg['execute'] does not exist.

                          I see now under msg.evenementServeurActe.actesNGAP, there are three child elements named acteNGAP. Each of those has has a child named execute and a child named NABMs. Two of the three already have a child named prestataire between execute and NABMs.

                          Will it always be the case that the first and only the first acteNGAP will be missing prestataire?

                          Is the prestataire section the same for every message (can you code it statically?)

                          Is the prestataire section the same for every acteNGAP section within the same message? Would you always be able to copy it from another section or could there sometimes be no prestataire in the entire message?

                          Code:
                          var prestataire = msg.evenementServeurActe.actesNGAP.acteNGAP.prestataire;
                          
                          if (prestataire.length() === 0) {
                              // build it
                              prestataire = <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>;
                          } else {
                              // copy the first one in the message
                              prestataire = prestataire[0].copy();
                          }
                          
                          for each (var acteNGAP in msg.evenementServeurActe.actesNGAP.acteNGAP) {
                              if (acteNGAP.prestataire.length() === 0) {
                                  acteNGAP.insertChildAfter(acteNGAP.execute[0], prestataire.copy());
                              }
                          }

                          Comment

                          Working...
                          X