Quick bit of xml assistance required.

Monkey Forums/Monkey Programming/Quick bit of xml assistance required.

Paul - Taiphoz(Posted 2015) [#1]
Sample of my code bellow along with a sample of my level xml, I'm trying to load all the aliens into a list but it seems to only be grabbing the first one it comes across then ignores the others..

What am I doing wrong here.

					Local i:XMLNode
					Repeat
						For i = EachIn node.GetChildren("alien")
							Local alienname:String
							Local alientype:Int
							Local time:Int
							
							alienname = i.GetAttribute("type")
							
							Select alienname
								Case "Grabber"
									alientype = ALIEN_GRABBER
								Case "Cargo"
									alientype = ALIEN_CARGO
								Case "Shooter"
									alientype = ALIEN_SHOOTER
							End Select
							
							time = int(i.GetAttribute("time"))
							
							AlienSpawner.Add(alientype, time)
						Next
					Until i.CountChildren = 0


<?xml version="1.0"?>
<world>
	<level name="Pluto" hint="Pluto, Micro Planetoid.">
		<wave id="1" hint="Wave One" timelimit="60">
			<huge rock="2" saphire="2" ruby="2" emerald="2"/>
			<large rock="2" saphire="2" ruby="2" emerald="2"/>
			<small rock="2" saphire="2" ruby="2" emerald="2"/>
			<alien type="Grabber" time="1000"/>
			<alien type="Cargo" time="2000"/>
			<alien type="Shooter" time="3000"/>
			<alien type="Grabber" time="4000"/>
			<alien type="Cargo" time="5000"/>
			<alien type="Grabber" time="6000"/>
			<alien type="Cargo" time="7000"/>
			
		</wave>



therevills(Posted 2015) [#2]
I would change your XML to have more parent nodes:
<?xml version="1.0"?>
<world>
	<level name="Pluto" hint="Pluto, Micro Planetoid.">
		<wave id="1" hint="Wave One" timelimit="60">
			<rocks>
				<rock type="huge" rock="2" saphire="2" ruby="2" emerald="2" />
				<rock type="large" rock="2" saphire="2" ruby="2" emerald="2" />
				<rock type="small" rock="2" saphire="2" ruby="2" emerald="2" />
			</rocks>
			<aliens>
				<alien type="Grabber" time="1000" />
				<alien type="Cargo" time="2000" />
				<alien type="Shooter" time="3000" />
				<alien type="Grabber" time="4000" />
				<alien type="Cargo" time="5000" />
				<alien type="Grabber" time="6000" />
				<alien type="Cargo" time="7000" />
			</aliens>
		</wave>
	</level>
</world>


Then you can traverse the <aliens> tag to create your objects.


Paul - Taiphoz(Posted 2015) [#3]
I did this...

						Next
						i.GetParent()
						i.GetNextSibling("alien")
					Until i.CountChildren = 0


I did this and it worked, not sure if its the best way tho your solution is probably a better one. adding it to my list of things to fix at a later time, its working now.. and THANKS for the quick reply mate im streaming atm and it would have held me up if I got stuck on it.


therevills(Posted 2015) [#4]
No worries... your code does look a bit strange, but as long as it works :)


Paul - Taiphoz(Posted 2015) [#5]
ok.. need a little more help here...

I went with your xml layout which is fine, but my code for parsing seems to load everything thats an alien in regardless of what branch its at. This is the first time I'v really worked with xml like this so forgive me if I'm being stupid.

		Local nodes:= doc.GetDescendants("level")'.GetDescendants("wave")

			nodes = doc.GetDescendants("wave")
			For node = EachIn nodes
				If node.GetAttribute("id") = _wave Then
					
					If _wave = 1 Then
						LevelHint.text2 = node.GetAttribute("hint")
						GameClock.SetDuration(Int(node.GetAttribute("timelimit")))
					Else
						LevelHint.text2 = LevelHint.text
						LevelHint.text = node.GetAttribute("hint")
						GameClock.SetDuration(Int(node.GetAttribute("timelimit")))
						
						GameClock.Start()
						ScreenShake = True
					EndIf
					
					nodes = doc.GetDescendants("rocks")
					For node = EachIn nodes
						For Local i:XMLNode = EachIn node.GetChildren("huge")
							Max_H_R[_level] = int(i.GetAttribute("rock"))
							
							Max_H_C[_level] = int(i.GetAttribute("saphire"))
							Max_H_G[_level] = int(i.GetAttribute("ruby"))
							Max_H_E[_level] = int(i.GetAttribute("emerald"))
						Next
						
						For Local i:XMLNode = EachIn node.GetChildren("large")
							Max_L_R[_level] = int(i.GetAttribute("rock"))
							Max_L_C[_level] = int(i.GetAttribute("saphire"))
							Max_L_G[_level] = int(i.GetAttribute("ruby"))
							Max_L_E[_level] = int(i.GetAttribute("emerald"))
						Next
						
						For Local i:XMLNode = EachIn node.GetChildren("small")
							Max_S_R[_level] = int(i.GetAttribute("rock"))
							Max_S_C[_level] = int(i.GetAttribute("saphire"))
							Max_S_G[_level] = int(i.GetAttribute("ruby"))
							Max_S_E[_level] = int(i.GetAttribute("emerald"))
						Next
					Next
					
					nodes = doc.GetDescendants("aliens")
					Print "aliens has " + nodes.Count + "nodes"
					For node = EachIn nodes
						For Local i:XMLNode = EachIn node.GetChildren("alien")
							Local alienname:String
							Local alientype:Int
							Local time:Int
														
							alienname = i.GetAttribute("type")
							
							Select alienname
								Case "Grabber"
									alientype = ALIEN_GRABBER
								Case "Cargo"
									alientype = ALIEN_CARGO
								Case "Shooter"
									alientype = ALIEN_SHOOTER
							End Select
							
							time = int(i.GetAttribute("time"))
							Print "Adding Alien " + alientype + " at interval " + time
							AlienSpawner.Add(alientype, time)
							
						Next
					Next
					
					'Print "Loaded "+AlienSpawnerList.Count()+ " aliens"
				EndIf
			Next



<world>
	<level name="Pluto" hint="Pluto, Micro Planetoid.">
		<wave id="1" hint="Wave One" timelimit="60">
			<rocks>		
				<huge rock="1" saphire="0" ruby="0" emerald="0"/>
				<large rock="0" saphire="0" ruby="0" emerald="0"/>
				<small rock="0" saphire="0" ruby="0" emerald="0"/>
			</rocks>
			<aliens>
				<alien type="Grabber" time="3000"/>
				<alien type="Cargo" time="6000"/>
				<alien type="Shooter" time="9000"/>		
			</aliens>
		</wave>
		
		<wave id="2" hint="Wave Two" timelimit="20">
			<rocks>		
				<huge rock="0" saphire="1" ruby="0" emerald="0"/>
				<large rock="0" saphire="0" ruby="0" emerald="0"/>
				<small rock="0" saphire="0" ruby="0" emerald="0"/>
			</rocks>
			<aliens>
				<alien type="Grabber" time="3000"/>
				<alien type="Cargo" time="6000"/>
				<alien type="Shooter" time="9000"/>		
			</aliens>
		</wave>



therevills(Posted 2015) [#6]
Hey Paul, dont be so hard on yourself.

I've expanded on the XML format to make it easier to use:


And heres an example:


Updated to include rocks

It should output the following:



Paul - Taiphoz(Posted 2015) [#7]
Holy fuck.... Just spent around the last hour and half trying to get your code to work with mine, in the end the only thing that was wrong was I forgot to add a <waves> branch to my xml LOL.. 3am, Time for some sleep I think..

Again mate thanks for the assist.

Had some nice viewers watching me code and draw tonight which was fun, think I will keep streaming and see how things go.


therevills(Posted 2015) [#8]
No worries :) I was watching you off and on... you were looking a bit tired in the end - LOL!


Paul - Taiphoz(Posted 2015) [#9]
Yeah rofl 4am before I finally fell asleep, you know your having fun when you totally and utterly lose track of time to the point where you start to hear the birds dawn chorus.
.