web stats
How do you parse a web service response? - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2013, 10:18 AM
rutledj rutledj is offline
Mirth Guru
 
Join Date: Nov 2010
Location: North Carolina
Posts: 386
rutledj is on a distinguished road
Default How do you parse a web service response?

I'm making a call to a webservice. In the 2nd destination I'm getting the response. How can I make that get sent to whereever without this data infront of the hl7 message:

SUCCESS: <?xml version="1.0" encoding="UTF-8" standalone="no"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><env:Header/><env:Body env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><m:getResultsResponse xmlns:m="http://medplus.com/resultsHub/observations"><result xmlns:n1="java:com.medplus.serviceHub.results.webs ervice" xsi:type="n1:ResultsResponse"><HL7Messages soapenc:arrayType="xsd:string[16]"><string xsi:type="xsd:string">MSH|^~\&amp;|LAB|HOU||213580 0|20091116165831|...


and this inbetween the MSH messages:

</string><string xsi:type="xsd:string">

There are multiple MSH messages in the returned response. I need to save them individually to a database.

Thanks,
Rut
Reply With Quote
  #2  
Old 01-30-2013, 10:45 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,116
narupley is on a distinguished road
Default

It sounds like you're using the .toString() function of the response object, which happens if you reference it like this:

Code:
responseMap.get('Destination 1')
Or:

Code:
$r('Destination 1')
Instead, you can return just the message of the response object like so:

Code:
var response = $r('Destination 1').getMessage();
Then you can convert it into an E4X XML object:

Code:
var rsp = new XML(response);
From there, you can iterate through nodes and extract the data that you need. Make sure to handle namespaces, either by setting a default, using Namespace objects, or both.
__________________
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
  #3  
Old 01-30-2013, 10:48 AM
rutledj rutledj is offline
Mirth Guru
 
Join Date: Nov 2010
Location: North Carolina
Posts: 386
rutledj is on a distinguished road
Default

I think that may be part of my problem. I'm not too much up on using namespaces to parse the xml.

Is there a simple way to take the response and pull out each complete hl7 message?

Thanks
Reply With Quote
  #4  
Old 01-30-2013, 02:10 PM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,116
narupley is on a distinguished road
Default

Quote:
Originally Posted by rutledj View Post
I'm making a call to a webservice. In the 2nd destination I'm getting the response. How can I make that get sent to whereever without this data infront of the hl7 message:

SUCCESS: <?xml version="1.0" encoding="UTF-8" standalone="no"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><env:Header/><env:Body env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><m:getResultsResponse xmlns:m="http://medplus.com/resultsHub/observations"><result xmlns:n1="java:com.medplus.serviceHub.results.webs ervice" xsi:type="n1:ResultsResponse"><HL7Messages soapenc:arrayType="xsd:string[16]"><string xsi:type="xsd:string">MSH|^~\&amp;|LAB|HOU||213580 0|20091116165831|...


and this inbetween the MSH messages:

</string><string xsi:type="xsd:string">

There are multiple MSH messages in the returned response. I need to save them individually to a database.

Thanks,
Rut
Yes. Like I said, get the message of the Response object, convert it to an E4X XML object, and then iterate through each node as necessary:

Code:
rsp = new XML($r('Destination 2').getMessage());
var env = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
var m = new Namespace('http://medplus.com/resultsHub/observations');
for each (str in rsp.env::Body.m::getResultsResponse.result.HL7Messages.string)
	// Insert into a database, etc.
__________________
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
  #5  
Old 01-31-2013, 10:59 AM
rutledj rutledj is offline
Mirth Guru
 
Join Date: Nov 2010
Location: North Carolina
Posts: 386
rutledj is on a distinguished road
Default

Thanks. This worked perfectly.

Rut
Reply With Quote
  #6  
Old 01-31-2013, 11:25 AM
rutledj rutledj is offline
Mirth Guru
 
Join Date: Nov 2010
Location: North Carolina
Posts: 386
rutledj is on a distinguished road
Default

But now to throw a kink into the works. I'm supposed to use a different call to the webservice that actually returns the same data but the hl7 messages are byte encoded. How can I use the above code and also decode them back to a string?

Rut
Reply With Quote
  #7  
Old 02-01-2013, 08:02 AM
rutledj rutledj is offline
Mirth Guru
 
Join Date: Nov 2010
Location: North Carolina
Posts: 386
rutledj is on a distinguished road
Default

Ok. I have this working now and decoding the message. In the transformer step I have this code:

msgReturned = new XML(responseMap.get('Destination 1').getMessage());

var env = new Namespace('http://schemas.xmlsoap.org/soap/envelope/');
var m = new Namespace('http://medplus.com/resultsHub/observations');

for each (str in msgReturned.env::Body.m::getHL7ResultsResponse.res ult.HL7Messages.HL7Message.message)
{
//logger.info('in for');
var decoded;
decoded = org.apache.commons.codec.binary.Base64.decodeBase6 4(str);
//logger.info('decoded ' + decoded);

}



This is working fine. I can get the individual hl7 messages as a string in the for loop. Now how do I send them to the output of the channel as individual xml messages?

Thanks,
Rut
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 05:08 AM.


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