# Looping through xml file

**URL:** https://discourse.nodered.org/t/looping-through-xml-file/40302
**Category:** General
**Created:** [3 February 2021 11:41 UTC](https://discourse.nodered.org/t/looping-through-xml-file/40302 "2021-02-03T11:41:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![vikingsraven](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vikingsraven/32/49243_2.png) [@vikingsraven](https://discourse.nodered.org/u/vikingsraven)
#### Post date: [3 February 2021 11:41 UTC](https://discourse.nodered.org/t/looping-through-xml-file/40302/1 "2021-02-03T11:41:34Z")

</div>

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

```auto
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

```auto
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

```auto
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

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [3 February 2021 18:31 UTC](https://discourse.nodered.org/t/looping-through-xml-file/40302/2 "2021-02-03T18:31:24Z")

</div>

Hi Mark,

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

paste using ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d56639561f3c9fa2f21f979311d7d4edcc5a9ef1.png) so it will be correctly formatted.

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

---

<div class="post-metadata">

### Author: ![vikingsraven](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vikingsraven/32/49243_2.png) [@vikingsraven](https://discourse.nodered.org/u/vikingsraven)
#### Post date: [5 February 2021 11:02 UTC](https://discourse.nodered.org/t/looping-through-xml-file/40302/3 "2021-02-05T11:02:39Z")

</div>

@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

```auto
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

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [5 February 2021 12:40 UTC](https://discourse.nodered.org/t/looping-through-xml-file/40302/4 "2021-02-05T12:40:40Z")

</div>

> [@vikingsraven](#):
>
> i cant share the actual xml file as it has data on it which i really cant share

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)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

### Author: ![vikingsraven](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vikingsraven/32/49243_2.png) [@vikingsraven](https://discourse.nodered.org/u/vikingsraven)
#### Post date: [5 February 2021 13:32 UTC](https://discourse.nodered.org/t/looping-through-xml-file/40302/5 "2021-02-05T13:32:59Z")

</div>

@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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [19 February 2021 13:33 UTC](https://discourse.nodered.org/t/looping-through-xml-file/40302/6 "2021-02-19T13:33:39Z")

</div>

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