Sign in to follow this  
Desdemona

TinyXml

Recommended Posts

The format for my game's XML file is similar to this: <item id="12345"> <ac dt="int">10</ac> <dr dt="int">2</dr> <enc dt="int">1000</enc> <name dt="string">platemail tunic</name> </item> I wrote some code using MSXML to read in the data and create my game objects, but I'm moving to TinyXml because of some compatibility issues. I'm having trouble with navigating the XML properly with TinyXml. The (incomplete) code below is what I have right now. I'm just trying to get the data displayed in the right order; once I have that, creating the game objects will be easy.
LoadXml(doc.FirstChild(APPNAME));

bool CSDataset::LoadXml(TiXmlNode* node)
{
	if (node == 0)
		return false;

	TiXmlNode* sibling = node;

	do
	{
		sibling = sibling->NextSibling();	
	}
	while (LoadXml(sibling));

	cout << node->Value() << endl;

	return LoadXml(node->FirstChild());
}

The above code creates output that looks like this: item ac 10 dr 2 enc 1000 name platemail tunic This above code does not attempt to display the attributes, which is where my problem is (e.g. the dt="int"). Ideally, I want the output to be this: item id=12345 ac dt=int 10 dr dt=int 2 enc dt=int 1000 name dt=string platemail tunic I can get the attribute pairs from the XML, but I just cant seem to get everything in the correct order. Any help from someone who knows TinyXml would be greatly appriciated... Thanks Joe

Share this post


Link to post
Share on other sites
you need to iterate the attributes

off the top of my head...roughly something like

bool CSDataset::LoadXml(TiXmlNode* node)
{
if (node == 0)
return false;

TiXmlNode* sibling = node;

do
{
sibling = sibling->NextSibling();
}
while (LoadXml(sibling))


cout << node->Value() << endl;
if (TiXmlElement* pElement = node->ToElement())
{
if (TiXmlAttribute* pAttrib = pElement->FirstAttribute())
{
do
{
cout << pAttrib->Value() << endl;
}
while(pAttrib = pAttrib->Next())
}
}

return LoadXml(node->FirstChild());
}




should be enough to get you started

Share this post


Link to post
Share on other sites
Okay, that got me to where I needed to be in a couple seconds flat, thanks a lot. That was only slightly different than one of the ways I had tried before, but I just couldnt get it right.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this