Announcement

Collapse
No announcement yet.

Parsing Response xml

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

  • Parsing Response xml

    I'm trying to get elements from response xml. I keep getting that the variable is undefined when I try to split it, but it has xml as its value. The following code is in a javascript destination after the destination that populates ResponseMsgD2. Any ideas as to why this is failing with undefined?

    Code:
    if($('ResponseMsgD2')!=""){
    	var rsp = $('ResponseMsgD2').toString();
    	var error = rsp.split("<Errors>")[1].split("</Errors>")[0];
    	channelMap.put('ServiceError',Error);
    }

  • #2
    you may have to parse the response.
    i.e. JSON.parse($('ResponseMsgD2'));
    and use logger statements to track what's happening: ie.
    Code:
    if($('ResponseMsgD2')!=""){
    	var rsp = $('ResponseMsgD2').toString();
            var parsed_rsp = JSON.parse($('ResponseMsgD2'));
    logger.info('rsp: ' + rsp + ', ' + 'parsed: ' + parsed_rsp);
    	var error = rsp.split("<Errors>")[1].split("</Errors>")[0];
    	channelMap.put('ServiceError',Error);
    }

    Comment


    • #3
      Hello,


      if you want the response in the Destination it self you do:


      Code:
      // Return a String with the response (XML/JSON/etc)
      
      var resp = response.getMessage();

      if you are in a different Destination and want to access the response you do:


      Code:
      // Return a String with the response (XML/JSON/etc)
      
      
      var resp = $('destination').getMessage();

      and then you can parse to XML or JSON:


      Code:
      // XML
      var xml = new XML(resp);
      
      
      // JSON
      var json = JSON.parse(resp);
      Best Regards,
      Alex Neiva

      Comment


      • #4
        Thanks for the responses. I tried the following as well, but I still get undefined when I try to check the string.

        Code:
        if($('ResponseMsgD9')!=""){
        	var rsp = new XML($('ResponseMsgD9'));
        	var error = rsp.Errors[0];
        channelMap.put('ServiceErrorString', rsp.Errors[0]);

        As I said before, I can see that the ResponseMsgD9 has xml in it and that the Errors element has data in it. I'm not sure why it always comes back with undefined when I try to use ResponseMsgD9.

        Comment


        • #5
          Try this instead:

          Code:
          var rsp = new XML($('ResponseMsgD9').getMessage());
          The value in the response map is a Response: http://javadocs.mirthcorp.com/connec.../Response.html
          Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

          Nicholas Rupley
          Work: 949-237-6069
          Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


          - How do I foo?
          - You just bar.

          Comment


          • #6
            Here's how I currently have it set up.

            In destination 9's Response Transformer:

            Code:
            channelMap.put('ResponseMsgD9', response.getMessage());

            In a later destination JavaScript Writer:

            Code:
            if($('ResponseMsgD9')!=""){
            	var rsp = new XML($('ResponseMsgD9'));
            	var error = rsp.Errors[0];
            channelMap.put('ServiceErrorString', rsp.Errors[0]);

            The message looks to be correct when I look at the mapping, it's just that if I try to use it to get an element I get undefined. Maybe I'm overlooking something small here?

            Comment


            • #7
              What actual error stacktrace are you getting?
              Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

              Nicholas Rupley
              Work: 949-237-6069
              Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


              - How do I foo?
              - You just bar.

              Comment


              • #8
                I'm not getting any errors. If I try to do any comparisons or try to get anything out of the xml object that I've mapped into 'ResponseMsgD9', it just comes back with undefined. My goal is to have the actual xml element mapped. With the code I just shared, the value of 'ServiceErrorString' is 'undefined', but I can see that there is text in the Errors element. I've tried a few ways now to try to pull anything from ResponseMsgD9, but I keep getting undefined.

                Comment


                • #9
                  I got it to work with the following code. Thanks for the help.


                  Code:
                  if($('ResponseMsgD9')!=""){
                  	var rsp = new XML($('ResponseMsgD9'));
                  
                  	var success = false;
                  	var errorString = false;
                  	try{
                  	errorString = rsp.toString().split("<a:Errors>")[1].split("</a:Errors>")[0];
                  	error = true
                  	}
                  	catch(Exception){ 
                  		error =false;
                  		errorString = false;
                  	}
                  	channelMap.put('ServiceErrorString', errorString);
                  	channelMap.put('ServiceError',error);
                  }

                  Comment

                  Working...
                  X