XML parser mergers two XML objects from list?


(John Andersson) #1

part of the parser:

for each (var objectgroup:XML in mapXML.objectgroup)
{
var groupname:String = objectgroup.attribute("name");
switch (groupname)
{
	case "Spawn": 
		for each (var object:XML in objectgroup.object)
		{
			var objectname:String = objectgroup.object.attribute("name");
			trace(objectname)
			switch (objectname)
			{
				case "Hero": 
					FP.world.add(new Hero(new Point(int(object.@x * 8), int(object.@y * 8))));
					break;
							
				case "Goblin_Thief": 
					FP.world.add(new Goblin_Thief(new Point(int(object.@x * 8), int(object.@y * 8))));
					break;
							
				default: 
					trace("no spawns")
			}
		}
}
}

XML file:

  <object name="Wooden" x="160" y="80" width="16" height="16"/>
 </objectgroup>
 <objectgroup name="Spawn" width="15" height="9">
  <object name="Hero" x="32" y="96" width="16" height="16"/>
  <object name="Goblin_Thief" x="112" y="80" width="16" height="16"/>
 </objectgroup>```

The trace gives me "HeroGoblin_Thief" twice, not "Hero" then "Goblin_Thief". Why is this? It's driving me insane, I can't spawn the enemy and hero anymore. I have no idea why this happened. It worked previously!

(azrafe7) #2

Don’t know for sure, but you might be forgetting some break; in your outer switch/case.

If not then try to isolate the problem by writing a simpler test and post it here if you still have issues.


(John Andersson) #3

I can’t seem to fix it, I’ve added breaks where they could be, but it still doesn’t change the wrong tracing. I think it’s more about how the XML file is built, what do you say?

This is as simple as it can get!


(Ultima2876) #4

Maybe a name clash? Complete stab in the dark:

for each (var obj:XML in objectgroup.object)

Try that instead?

EDIT: Then again, if it worked previously it indicates that something you’ve changed somewhere else in your code has mucked it up. Do you use version control at all (svn, git)? If you do then it’s worth reverting and seeing when it broke and what broke it.


(John Andersson) #5

I sadly don’t have a previous version available :frowning:


(Jacob Albano) #6

I reduced your example to the smallest it could be and it works fine:

var mapXML:XML =
<objectgroup name="Spawn" width="15" height="9">
    <object name="Hero" x="32" y="96" width="16" height="16"/>
    <object name="Goblin_Thief" x="112" y="80" width="16" height="16"/>
</objectgroup>;

for each (var object:XML in mapXML.object)
{
    var objectname:String = object.attribute("name");
    trace(objectname)
}

(John Andersson) #7

Thank you so much!!!


(Zachary Lewis) #8

Get your E4X on, brother.

for each (var node:XML in mapXML.object)
{
  trace(node.@name);
}

(Jacob Albano) #9

Yeah that’s my preferred approach. :wink: I didn’t want to change it too much or it might be more confusing than helpful.

Related:

trace(node.@name);
trace(node["@name"]);
trace(node.@["name"]);

All identical. It’s a beautiful thing.