Announcement

Collapse
No announcement yet.

Iterating XML input file

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

  • Iterating XML input file

    I am trying to learn how to iterate in Mirth, particularly the incoming XML file that I am translating/mapping to HL7. There are several examples of iteration for the HL7, but they are not working for me in XML. I know several programming languages, but Java is not my main one (Delphi is current), so this may simple but is stumping me so far.

    In the source transformer I have
    Code:
    var i = 0;
    
    for each (Insured in msg..Insureds){
    i++;
    }
    channelMap.put("TotalInsureds",i);
    Which gives me 1.0, every time.

    If I try
    Code:
    var i = 0
    for each (segment in msg.children() ){
    if(segment.name() == "Insured"«»){
    i++;
    }
    }
    channelMap.put("TotalInsureds",i);
    I get 0

    Other variations that produce 1 include
    Code:
    for each (msg['Appt']['Insureds']['Insured'] in msg['Appt']['Insureds']){
    i++;
    }
    and
    Code:
    for each (Insured in msg['Appt']['Insureds']){
    i++;
    }
    Simplified XML looks like

    Code:
    <?xml version="1.0" encoding="windows-1252"?>
    <Appts>
      <Appt>
        <HL7>
          <MessageType>SIU</MessageType>
          <TriggerEvent>S14</TriggerEvent>
        </HL7>
        <Patient>
          <LOCATION>002</LOCATION>
          <BILLUNIQUE>3</BILLUNIQUE>
          <FAM_ID>A</FAM_ID>
          <LAST_MOD>02/06/2009</LAST_MOD>
          <LAST>McGillicutty</LAST>
          <FIRST>Francine</FIRST>
          <MI/>
        </Patient>
        <Insureds>
          <Insured>
            <INSUNIQUE>9</INSUNIQUE>
            <ID>1</ID>
            <CO_TYPE>Commercial</CO_TYPE>
            <CO_NAME>AARP</CO_NAME>
            <CO_STRT>PO Box 1011</CO_STRT>
            <CO_CITY>Montgomeryville</CO_CITY>
            <CO_PHONE>(201)222-3566</CO_PHONE>
          </Insured>
          <Insured>
            <INSUNIQUE>10</INSUNIQUE>
            <ID>2</ID>
            <CO_TYPE>Commercial</CO_TYPE>
            <CO_NAME>ATT Technologies Inc</CO_NAME>
            <CO_STRT>2100 Adams Avenue, Suite 100</CO_STRT>
            <CO_CITY>Baltimore</CO_CITY>
            <CO_PHONE>(301)980-9333</CO_PHONE>
          </Insured>
          <Insured>
            <INSUNIQUE>11</INSUNIQUE>
            <ID>3</ID>
            <CO_TYPE>Commercial</CO_TYPE>
            <CO_NAME>Eyefinity</CO_NAME>
            <CO_STRT>35 Iron Point Circle</CO_STRT>
            <CO_CITY>Folsom</CO_CITY>
            <CO_PHONE>(877)448-0707</CO_PHONE>
          </Insured>
        </Insureds>
        <Doctor>
          <ID>SQS</ID>
          <LAST>Surgeon</LAST>
          <FIRST>Sam</FIRST>
          <MI>Q</MI>
          <SALUTE>Dr.</SALUTE>
          <DEGREE>M.D.</DEGREE>
        </Doctor>
        <PATUNIQUE>3</PATUNIQUE>
        <LAST_MOD>01/12/2009</LAST_MOD>
        <APPTUNIQUE>3</APPTUNIQUE>
        <ADD_LOC>002</ADD_LOC>
        <DATE>01/12/2009</DATE>
        <TIME>13:30</TIME>
        <LOCATION>002</LOCATION>
        <ID>SQS</ID>
      </Appt>
    </Appts>
    I'd greatly appreciate any help.
    Doug

  • #2
    Re:Iterating XML input file

    OK, figured it out by reading the Ecma-357 manual. And, yes, it was simple.

    I was reading the in part incorrectly. I had thought that

    in Insureds

    would iterate through what it contained. Not the case.

    for each (x in msg..foo){bar;}

    means look at everything that looks like foo, assign it to x, then deal with it. I had believed it meant look for every x that is in foo.

    So my wrong statement
    Code:
    for each (Insured in msg..Insureds){
    i++;
    }
    works just fine with
    Code:
    for each (Insured in msg..Insured){
    i++;
    }
    and I will probably change it to not be the variable Insured just to make it less confusing to those who follow behind.
    Hope this helps other noobs.

    Comment


    • #3
      Re:Iterating XML input file

      I recommend you to read the examples a w3school:
      http://www.w3schools.com/e4x/default.asp

      And if you want to more complex tasks with XML files, you may use XSL transformations instead of JS code

      Comment


      • #4
        Re:Iterating XML input file

        Thanks, that was a good basic reference.

        Now I'm trying to pass a subset of the XML from the Source to the Destination(s) with a Channel variable.

        The following works to get it to "the other side"

        in the Source Tranformer

        Code:
        var InsuredArray = new XMLList(msg..Insured);
        channelMap.put("AllInsureds",InsuredArray);
        However, on the destination transformer side, pretty much every combination I've tried so far with $('AllInsureds') has failed with
        ERROR MESSAGE: Error evaluating transformer

        com.webreach.mirth.model.converters.SerializerExce ption: java.lang.ArrayIndexOutOfBoundsException: 1


        Any clues would be appreciated, and I will, of course, continue to doggedly muddle forward.

        Post edited by: dugjohnson, at: 02/10/2009 11:41

        Comment


        • #5
          Re:Iterating XML input file

          I found another tutorial resource with more depth, but not as "techie" as the E4X 100 page manual. Very understandable and useful.
          https://developer.mozilla.org/En/E4X_Tutorial

          Comment


          • #6
            Re:Iterating XML input file

            Got it all figured out and I'm posting it here for the use of anyone who might find it helpful. I am NOT a whiz on this, so take it with a grain of salt, YMMV, etc. However this is working.

            On the Source transformer side, to send a group of Insured in a single channel variable I used the following
            Code:
            var InsuredArray = msg..Insured;
            channelMap.put("AllInsureds",InsuredArray);
            This sends any XML that is inside an Insured tag to the Channel variable AllInsured, and it is, for all intents and purposes, an XMLList.

            On the Destination transformer side
            Code:
            var x = 0;
            
            for (var i = 0; i < $('AllInsureds').length();i++){
            var newIN1 = new XML('<IN1></IN1>');
            x = i+1;
            newIN1['IN1.1']['IN1.1.1'] = x.toString(); 
            newIN1['IN1.2']['IN1.2.1'] = $('AllInsureds')[i].PLAN.toString(); //Plan ID
            newIN1['IN1.3']['IN1.3.1'] = $('AllInsureds')[i].IGRPUNIQUE.toString(); //Company ID
            newIN1['IN1.4']['IN1.4.1'] = $('AllInsureds')[i].CO_NAME.toString();
            newIN1['IN1.5']['IN1.5.1'] = $('AllInsureds')[i].CO_STRT.toString();
            newIN1['IN1.5']['IN1.5.3'] = $('AllInsureds')[i].CO_CITY.toString();
            newIN1['IN1.5']['IN1.5.4'] = $('AllInsureds')[i].CO_STATE.toString();
            newIN1['IN1.5']['IN1.5.5'] = $('AllInsureds')[i].CO_ZIP.toString();
            var ID = $('AllInsureds')[i].ID.toString();
            if (ID =='1'||ID =='2'||ID =='3'){
            newIN1['IN1.22']['IN1.22.1'] = ID;  //Coordination of benefits
            }
            if (i == 0){
            tmp['IN1'][0]=newIN1;}
            else{
            tmp['IN1']+= newIN1; }
            }
            This creates a new XML IN1 variable which I fill in by iterating through the AllInsureds based on it's length, which in the case of an XMLList, is its count.
            Potential gotchas ('cause they got me) is that you MUST have the toString() on each element, even though the XMLList tutorial I read says that E4X forces it to string.

            (Yes, I know PLAN and IGRPUNIQUE aren't in the XML I posted originally. That was a simplified version of the original XML. All of the XML segments are there in the actual file. No magic was involved.)

            Note that this also allows you to add segments on the fly, and is an adaptation of another forum posting.

            The section that checks for the first iteration works because I have an IN1 segment in the template. If that weren't there, you'd have to create it instead. I'll worry about that another day/night.

            Post edited by: dugjohnson, at: 02/10/2009 20:32

            Comment

            Working...
            X