web stats
how do I pull out a single xml attribute when it repeats - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 11-30-2016, 05:16 AM
klundell klundell is offline
OBX.3 Kenobi
 
Join Date: Sep 2010
Posts: 154
klundell is on a distinguished road
Default how do I pull out a single xml attribute when it repeats

using mirth 3.2.2.7694 I have the following address in xml. I just want to pull out the first attribute which would be "842 New York Ave #8 Osbourne Home"

My code for parsing the attributes is as follows which works for single attributes but apparently not for repeating attributes.

address = msg['personalInformation'].*::['person'].*::['address'].*::['streetAddress'].toString() ;

I though perhaps the following might work but it didn't

msg['personalInformation'].*::['person'].*::['address'][0].*::['streetAddress'][0].toString() ;

Any idea on how to get just the first attribute?



<all0:streetAddress xmlns:all0="http://www.allscripts.com/schema/cedExtract/baseTypes/2009/10/21">842 New York Ave #8 Osbourne Home</all0:streetAddress><all0:streetAddress xmlns:all0="http://www.allscripts.com/schema/cedExtract/baseTypes/2009/10/21">Most Eastern home on land </all0:streetAddress> <all0:streetAddress xmlns:all0="http://www.allscripts.com/schema/cedExtract/baseTypes/2009/10/21">842 New York Ave #8 Osbourne Home</all0:streetAddress><all0:streetAddress xmlns:all0="http://www.allscripts.com/schema/cedExtract/baseTypes/2009/10/21">Most Eastern home on land </all0:streetAddress>
Reply With Quote
  #2  
Old 11-30-2016, 08:41 AM
obsessio obsessio is offline
Mirth Guru
 
Join Date: Mar 2015
Location: Turkey
Posts: 177
obsessio is on a distinguished road
Default

For you sample XML;
PHP Code:
msg.*::['streetAddress'][0].toString()
msg.*::['streetAddress'][1].toString()
msg.*::['streetAddress'][2].toString()
msg.*::['streetAddress'][3].toString() 
For your code sample;
PHP Code:
msg['personalInformation'].*::['person'].*::['address'].*::['streetAddress'][0].toString();
msg['personalInformation'].*::['person'].*::['address'].*::['streetAddress'][1].toString();
msg['personalInformation'].*::['person'].*::['address'].*::['streetAddress'][2].toString();
msg['personalInformation'].*::['person'].*::['address'].*::['streetAddress'][3].toString(); 
Reply With Quote
  #3  
Old 11-30-2016, 08:43 AM
obsessio obsessio is offline
Mirth Guru
 
Join Date: Mar 2015
Location: Turkey
Posts: 177
obsessio is on a distinguished road
Default

By the way, please check transformer drag & drop code generation. It will help you. Because your last 6 topics related this.
Reply With Quote
  #4  
Old 12-03-2016, 11:52 AM
klundell klundell is offline
OBX.3 Kenobi
 
Join Date: Sep 2010
Posts: 154
klundell is on a distinguished road
Default

Thank you.

I am having another challenge where I need to parse part of the xml programatically I have written the following code but don't really know how to access the children in the xml

I'm getting each segment back but how do I get the children all the way down to the value I want

//Grab ENTIRE message from variable set in mappings (transformer).
//Once we have that message we must wrap it in XML so that we can gain access to child segments

var mymsg = $('mymessage');
var mainmsg = new XML(mymsg);

var count = 0;
for each (seg in mainmsg.children())
{
logger.info('SEGMENT NAME ' + seg.name().toString());

var elem = mainmsg.getElementById('medication');
var child = elem.firstElementChild;

logger.info('element name is: ' + elem.toString());
logger.info('child is: ' + child);
}

My XML is the following structure
XML-Message (1.0)
documentContents
xmlBody
medications
medications (multiple)
patientInstructions
1-2mg PO, q4h PRN for anxiety
Reply With Quote
  #5  
Old 12-03-2016, 12:33 PM
klundell klundell is offline
OBX.3 Kenobi
 
Join Date: Sep 2010
Posts: 154
klundell is on a distinguished road
Default

I thought maybe the below code would work but it through an error saying "can't call method toString() of undefined"

logger.info(' child name is ' + mainmsg['documentContents'].*::['xmlBody'].*::['medications'].*::['medication'].*::['patientInstructions'][1].toString());

Also in my for each statement ( for each (seg in mainmsg.children() ) I don't really want to loop through the top level elements I want to loop through the medication repeats. Hopefully that makes sense.

thanks in advance for any help.
Reply With Quote
  #6  
Old 12-03-2016, 12:48 PM
klundell klundell is offline
OBX.3 Kenobi
 
Join Date: Sep 2010
Posts: 154
klundell is on a distinguished road
Default

I thought maybe the following would work but apparently getElementByTagName is undefined?


var mynodes = getElementByTagName('medication');

var count = 0;
//for each (seg in mainmsg.children())
for (count; count < mynodes.length; count++ )
{
logger.info('SEGMENT NAME ' + mynode.name().toString());
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 02:59 AM.


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