web stats
iterate over all nodes - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 08-01-2012, 02:26 AM
wello007 wello007 is offline
OBX.3 Kenobi
 
Join Date: Dec 2007
Posts: 160
wello007 is an unknown quantity at this point
Default iterate over all nodes

Hello,

I need to iterate over all nodes of an xml message to add attributes. do you know how I can do it in javascript?
Reply With Quote
  #2  
Old 08-01-2012, 04:25 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,360
cory_cole is on a distinguished road
Default

for each (node in msg)
{
}
Reply With Quote
  #3  
Old 08-01-2012, 07:43 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,126
narupley is on a distinguished road
Default

If the E4X object you want to iterate through is basically a bunch of sibling nodes, then "for each (node in object)" will get you what you want. However, if you want to iterate through the child nodes of a particular object, then you'll need to use "for each (node in object.children())". Or, you can account for both cases at once:

Code:
for each (node in object) {
	for each (child in node.children()) {
		// Do something
	}
}
Or:

Code:
for each (node in object) {
	for each (child in node.elements()) {
		// Do something
	}
}
Alternatively, you can take a single E4X object and recursively iterate through all descendant nodes, including inner text elements:

Code:
for each (element in object.descendants()) {
	// Do something
}
__________________
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
  #4  
Old 08-02-2012, 01:25 AM
wello007 wello007 is offline
OBX.3 Kenobi
 
Join Date: Dec 2007
Posts: 160
wello007 is an unknown quantity at this point
Default

I did that but I don't the structure of the received xml can be different so i dont' know the depth. I will need to do recursive function.
Merci Narupley
Reply With Quote
  #5  
Old 08-02-2012, 04:18 AM
wello007 wello007 is offline
OBX.3 Kenobi
 
Join Date: Dec 2007
Posts: 160
wello007 is an unknown quantity at this point
Default

Quote:
Originally Posted by cory_cole View Post
for each (node in msg)
{
}
This will not loop through childs
Reply With Quote
Reply

Tags
iterate, xml child segments

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:19 PM.


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