Looping through xml file

HI , noob to javascript and node-red , but loving it. managed to get a complex bit of code running but now im stuck on possibly a really simple issue.
so i have an xml file that i need to pull some data off ( badly formated! but its wha ti have to work with!).
im trying to some waypoints, i know the number of waypoints in total from a function

var wayPointCount = {payload: msg.payload.Configuration.MissionManager[0].WptTrack_0[0].NoWayPts[0]};

this returns a number as a string, which i think is ok , ill sort that bit i can always add a count value to test
anyway i need to grab the section i need from the xml

var wayPoints = {payload:msg.payload.Configuration.MissionManager[0].WptTrack_0[0].WayPt_0[0].LatLong};

so i need too change the WayPt_0 to WayPt_X for example and loop though and change X.
like i said its probably really simple and the answer is staring me in the face, but i seem to have gone code blind!
Im thinking someway along these lines

var wayPointCount = {payload: msg.payload.Configuration.MissionManager[0].WptTrack_0[0].NoWayPts[0]};
for (; i < wayPointCount; ) {
var wayPoints = {payload: msg.payload.Configuration.MissionManager[0].WptTrack_[i][0].NoWayPts[0]};
  i++;
return wayPoints;
} 

any help would be gratfully recieved
thanks

Hi Mark,

can you share the xml in order to test and make recommendations on how to process it with javascript.

paste using image so it will be correctly formatted.

can you also describe what the end goal is .. you want an array of LatLong wayPoint values ?

@UnborN ,
i cant share the actual xml file as it has data on it which i really cant share, but i have put in order what it is set up

object
Configuration: object
	MissionManager: array[1]
		0: object
			NoWaypoints: array[1]
			WayPt_0: array[1]
			NoTracks: array[1]
			WptTrack_0: array[1]
				0: object
					Name: array[1]
						0: "TRACK 1"
					NoWayPts: array[1]
						0: "4"
					WayPt_0: array[1]
						0: object
							CollAvoidParams_0: array[1]
							Name: array[1]
							LatLong: array[1]
								0: object
									Lat: array[1]
									Long: array[1]
							ShowName: array[1]
							UseDepth: array[1]
							UseSpeed: array[1]
							Depth: array[1]
							Speed: array[1]
							Radius: array[1]
					WayPt_1: array[1]
					WayPt_2: array[1]
					WayPt_3: array[1]
					bXParams: array[1]
					XP_uSegAcq0: array[1]
					XP_uSegAcqNode0: array[1]
					XP_uSegAcqNodeResume: array[1]
					XP_uSegAcqResume: array[1]
					XP_uTrackEndOption: array[1]
					XP_uTrackInit0: array[1]
					XP_uTrackInitResume: array[1]
			Target0Name: array[1]
			SafeBoundary: array[1]

so in the xml there are various waypoints, the supplier software generates a new waypoint set per way point , so this file has 1 WptTrack_0 and this has 4 waypoints in it.
I need to iterate through WayPt_0 to WayPt_3 and then pull the lat and long values which then have to be decipherd ( thats bits done!)
the path i have to the lat is payload.Configuration.MissionManager[0].WptTrack_0[0].WayPt_0[0].LatLong[0].Lat
and i can read and convert that, its the iterating of the WptTrack_X that im having issues with.
as the next waypoint info is at
payload.Configuration.MissionManager[0].WptTrack_0[0].WayPt_1[0].LatLong[0].Lat
i hope this helps, like i said i cant give the xml out and i dont have a test one at present.
thanks
Mark

You could simply and carefully edit out all the data you cant share and replace the fields with fake data.
Its little difficult to test without the complete structure but i tried to recreate parts of it.

Maybe you can use Jsonata query expression in a Change node to filter out the Lat and Long values (but you havent explained how you want the final msg)

Here is an example flow that puts the values in an array (jsonata can take all Lat and Long and **. means at any level deep in your original object)

[{"id":"fcf9aa91.83c648","type":"inject","z":"94ab22aa.035ba","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Configuration\":{\"MissionManager\":[{\"NoWaypoints\":[],\"WayPt_0\":[],\"NoTracks\":[],\"WptTrack_0\":[{\"Name\":[\"TRACK 1\"],\"NoWayPts\":[\"4\"],\"WayPt_0\":[{\"CollAvoidParams_0\":[],\"Name\":[],\"LatLong\":[{\"Lat\":[11],\"Long\":[22]}],\"ShowName\":[],\"UseDepth\":[],\"UseSpeed\":[],\"Depth\":[],\"Speed\":[],\"Radius\":[]}],\"WayPt_1\":[{\"CollAvoidParams_0\":[],\"Name\":[],\"LatLong\":[{\"Lat\":[33],\"Long\":[44]}],\"ShowName\":[],\"UseDepth\":[],\"UseSpeed\":[],\"Depth\":[],\"Speed\":[],\"Radius\":[]}],\"WayPt_2\":[{\"CollAvoidParams_0\":[],\"Name\":[],\"LatLong\":[{\"Lat\":[55],\"Long\":[66]}],\"ShowName\":[],\"UseDepth\":[],\"UseSpeed\":[],\"Depth\":[],\"Speed\":[],\"Radius\":[]}],\"WayPt_3\":[{\"CollAvoidParams_0\":[],\"Name\":[],\"LatLong\":[{\"Lat\":[77],\"Long\":[88]}],\"ShowName\":[],\"UseDepth\":[],\"UseSpeed\":[],\"Depth\":[],\"Speed\":[],\"Radius\":[]}],\"bXParams\":[],\"XP_uSegAcq0\":[],\"XP_uSegAcqNode0\":[],\"XP_uSegAcqNodeResume\":[],\"XP_uSegAcqResume\":[],\"XP_uTrackEndOption\":[],\"XP_uTrackInit0\":[],\"XP_uTrackInitResume\":[]}],\"Target0Name\":[],\"SafeBoundary\":[]}]}}","payloadType":"json","x":240,"y":1220,"wires":[["ab01cbf5.49d438"]]},{"id":"6991712e.b0fb9","type":"debug","z":"94ab22aa.035ba","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":1220,"wires":[]},{"id":"ab01cbf5.49d438","type":"change","z":"94ab22aa.035ba","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"Lat\": **.Lat, \"Long\": **.Long}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":1220,"wires":[["6991712e.b0fb9"]]}]

@UnborN
thank you for this, i was just looking at jasonata.
Yeah also thought about doing the fake data as well, just been busy since i replied, thank you so much for your help, this does exactly what i needed.
really appreciate your help on this, no doubt ill be back on here at some point , the more im using node-red the more powerful it seems.
Thanks again

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.