XML. (was: Re: [ODE] outputting objects)

Chris Brodie Chris.Brodie at macquarie.com
Tue Feb 4 15:36:02 2003


Not that my answer is the best but it was really easy for me(being new at the time) and newbies to understand:

I use a 1::1 mapping between elements and objects.

Element = Object
Attribute = Member Variable

So something like this:

<Body Position="0 0 0" Type="Box" Angle="0 45 0">
	<Geometry Type="Box"/>
<Body>

To me this is intuitive. The parent child relationship is quite intuitive and this would map to code in game like(In my c++ engine just as an example):

class CBody
{

	SetAttributes(Pairs& a_Attribs);

private:
	CVector			m_Position;
	std::vector<CGeometry> 	m_Geometry;
}

The only thing left to work out is, how the hell did the attribs get set? The parser collcts the pairs in to a 
std::map<std::pair<std::string, std::string> > >. For non c++ people thats a hash table of string pairs. A pair would be : Position="0 0 0"

So all the SetAttributes would have to do is :

(
	if (Attribs.Get("Position", Value) == true)
		m_Position = CVector(Value); // My vector can turn a string in to the numerical representation
}

I've used this for quite a while and it all works a charm. As for how the Objects get created I use a Factory class. For C people thats just a fancy lookup table. The table looks up the objects token, for example "Body" and create an object of that type.

An example of the factory can be found here:

http://www.flipcode.com/cgi-bin/msg.cgi?showThread=COTD-TemplateFactory&forum=cotd&id=-1



Here is a simple little world file from my engine, just a test world to test lighting:

<World Gravity="3.71">
	<Camera Position="0 2 0" />
	<Light Style="Directional" Position="50 50 50" />
	<StaticEntity Position="0 1 -70" Angle="0 0 0" File="Structure\Gastanks\1.lwo" />
</World>


..well thats how I do it anyway.

HTH

Chris
http:\\fourth.flipcode.com



-----Original Message-----
From: Anselm Hook [mailto:anselm@hook.org]


I'd still argue that the point is still not the xml itself - it is that
you need some tool to map between *any* notation and C objects.  lets say
that you have an xml file:

  <ode:body>
     <position>0 0 0</position>
  </ode:body>

how do you pluck out and apply position to the ode object without having
to hand write code?  do you write code like this:


NOTICE
This e-mail and any attachments are confidential and may contain copyright material of Macquarie Bank or third parties. If you are not the intended recipient of this email you should not read, print, re-transmit, store or act in reliance on this e-mail or any attachments, and should destroy all copies of them. Macquarie Bank does not guarantee the integrity of any emails or any attached files. The views or opinions expressed are the author's own and may not reflect the views or opinions of Macquarie Bank.