XML problems (again) [SOLVED]


(TaylorAnderson) #1

I don’t know how to explain this problem in XML terms, but…I have this XML:

  <Entities>
    <Tree1 id="0" x="192" y="352" />
    <Tree2 id="1" x="320" y="288" />
    <Tree3 id="2" x="704" y="336" />
    <Tree4 id="3" x="32" y="400" />
    <Tree5 id="4" x="432" y="400" />
    <Grass1 id="5" x="384" y="448" />
    <Grass1 id="6" x="672" y="384" />
    <Grass1 id="7" x="240" y="400" />
    <Grass2 id="8" x="512" y="448" />
    <Flower1 id="9" x="128" y="448" />
    <Flower1 id="10" x="592" y="384" />
    <Flower2 id="11" x="80" y="448" />
    <Flower2 id="12" x="272" y="400" />
    <Flower2 id="13" x="416" y="448" />
    <Mushrooms1 id="14" x="544" y="192" />
    <Mushrooms1 id="15" x="352" y="192" />
    <Mushrooms1 id="16" x="224" y="96" />
    <Mushrooms2 id="17" x="32" y="80" />
    <Mushrooms2 id="18" x="256" y="80" />
    <Mushrooms2 id="19" x="416" y="176" />
    <Mushrooms2 id="20" x="768" y="240" />
    <Rock1 id="21" x="80" y="64" />
    <Rock2 id="22" x="464" y="144" />
    <Spiral id="23" x="160" y="80" />
    <Spiral id="24" x="576" y="176" />
    <Player id="25" x="16" y="448" />
  </Entities>

as you can see, there are a lot of entities. these entities are mostly all scenery, so I want to have a class that represents all of them. My problem is, usually the way tutorials teach Ogmo is that to get a list of elements you would have to do mapXML.Entities.Player, or some other specific name as the last dot. My question is, how do I just cycle through all of the nodes under Entities, and handle them appropriately that way?

Thanks, Taylor

PS. this is possibly a stupid question. I don’t know much about XML other than Ogmo stuff, mostly.


(Bora Kasap) #2

Use like that: (if you’re not using FP.getXML(). because i’m not, so i don’t know is that works with FP.getXML()"

<EntitiesList>
    <entities type="tree1" id="0" x="192" y="352" />
    <entities type="tree2" id="1" x="320" y="288" />
    <entities type="tree3" id="2" x="704" y="336" />
    <entities type="tree4" id="3" x="32" y="400" />
    <entities type="tree5" id="4" x="432" y="400" />
    <entities type="grass1" id="5" x="384" y="448" />
    <entities type="grass1" id="6" x="672" y="384" />
    <entities type="grass1" id="7" x="240" y="400" />
    <entities type="grass2" id="8" x="512" y="448" />
    <entities type="flower1" id="9" x="128" y="448" />
    <entities type="flower1"id="10" x="592" y="384" />
    <entities type="flower2"id="11" x="80" y="448" />
    <entities type="flower2"id="12" x="272" y="400" />
    <entities type="flower2"id="13" x="416" y="448" />
    <entities type="mushroom1" id="14" x="544" y="192" />
    <entities type="mushroom1" id="15" x="352" y="192" />
    <entities type="mushroom1" id="16" x="224" y="96" />
    <entities type="mushroom2" id="17" x="32" y="80" />
    <entities type="mushroom2" id="18" x="256" y="80" />
    <entities type="mushroom2" id="19" x="416" y="176" />
    <entities type="mushroom2" id="20" x="768" y="240" />
    <entities type="rock1" id="21" x="80" y="64" />
    <entities type="rock2" id="22" x="464" y="144" />
    <entities type="spiral" id="23" x="160" y="80" />
    <entities type="spiral" id="24" x="576" y="176" />
    <entities type="player" id="25" x="16" y="448" />
  </EntitiesList>

Then pull like that: (if your “EntitiesList is your XML file’s top level node”, no need to use it’s name when reading data, “xml.entities” is enough to read entities as a list)

var mushroomsarray:Array = new Array();
var entitiesxml:XMLList = xml.entities;
for each(var element:XML in entitiesxml)
{
  switch (element.@type)
  {
    case "mushroom1":
    mushroomsarray.push(FP.world.add(new Mushroom1(element.@x,element.@y)));
    break;
    case "mushroom2":
    mushroomsarray.push(FP.world.add(new Mushroom2(element.@x,element.@y)));
    break;
    case "mushroom3":
    mushroomsarray.push(FP.world.add(new Mushroom3(element.@x,element.@y)));
    break;
  }
}

(Alex Larioza) #3

@AlobarNon The OP mentioned he’s using Ogmo, so he doesn’t have control over the structure of XML file.

Here’s how I handle it:

for each (var o:XML in xmlData.Entities.children())
{
	switch(o.localName())
	{
		case 'Tree1':
			// stuff
			break;
		case 'Tree2':
			// stuff
			break;
		case 'Tree3':
			// stuff
			break;
		// ...and etc
	}
}

The key thing to note is the first of the for-each loop. By calling “xmlData.Entities.children()” you return a list of child nodes which you can then check names, which I do with the switch statement.


(TaylorAnderson) #4

Thank you!

I decided to tackle the whole problem a different way, but its likely I’ll need this information for later (and plus it provides a little more insight into how XML works, so I’ll consider the issue solved.


(Ultima2876) #5