web stats
Parsing Response xml - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 09-04-2018, 01:00 PM
drobertson drobertson is offline
Mirth Newb
 
Join Date: Aug 2018
Posts: 12
drobertson is on a distinguished road
Default 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);
}
Reply With Quote
  #2  
Old 09-04-2018, 03:01 PM
stormcel stormcel is offline
OBX.3 Kenobi
 
Join Date: Mar 2010
Location: Phoenix
Posts: 150
stormcel is on a distinguished road
Default

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);
}
Reply With Quote
  #3  
Old 09-05-2018, 01:12 AM
AlexNeiva AlexNeiva is offline
Mirth Guru
 
Join Date: Oct 2013
Location: Portugal
Posts: 275
AlexNeiva is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 09-05-2018, 06:46 AM
drobertson drobertson is offline
Mirth Newb
 
Join Date: Aug 2018
Posts: 12
drobertson is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 09-05-2018, 06:50 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,115
narupley is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 09-05-2018, 07:06 AM
drobertson drobertson is offline
Mirth Newb
 
Join Date: Aug 2018
Posts: 12
drobertson is on a distinguished road
Default

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?
Reply With Quote
  #7  
Old 09-05-2018, 07:13 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,115
narupley is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 09-05-2018, 07:22 AM
drobertson drobertson is offline
Mirth Newb
 
Join Date: Aug 2018
Posts: 12
drobertson is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 09-05-2018, 07:57 AM
drobertson drobertson is offline
Mirth Newb
 
Join Date: Aug 2018
Posts: 12
drobertson is on a distinguished road
Default

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);
}
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 07:14 PM.


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