3 Replies - 418 Views - Last Post: 23 January 2013 - 10:15 AM Rate Topic: -----

#1 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 998
  • Joined: 30-July 08

How to load new (unknown) items from a XML file.

Posted 23 January 2013 - 03:03 AM

I wonder if anyone can give me any idea how to load new (unknown) items I have inside a XML doc.
Our designer want the functionallity to be able to "mod" our game by just adding new items inside the XML doc instead of having a fix amout of stuff.

At the moment it looks like this

Before new stuff is added
<!--- some other stuff -->
<goods type="goods" name="food" tooltip="Used for traveling and in the home city" picture="food.jpg" value="5"/>
<!--- some more other stuff -->


After
<!--- some other stuff -->
<goods type="goods" name="food" tooltip="Used for traveling and in the home city" picture="food.jpg" value="5"/>
<goods type="goods" name="coal" tooltip="do something" picture="coal.jpg" value="25"/>
<!--- some more other stuff -->

The designer want to be able to go into the xml document and add a new goods without being forced to edit the code as well. But I have no clue how to do this or even how to think about it to solve it.

Anyone here that has an idea how this can be achived?

This post has been edited by FrozenSnake: 23 January 2013 - 03:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to load new (unknown) items from a XML file.

#2 .Aaron  Icon User is offline

  • Rock, paper, scissors, lizard, Spock
  • member icon

Reputation: 62
  • View blog
  • Posts: 2,907
  • Joined: 04-August 09

Re: How to load new (unknown) items from a XML file.

Posted 23 January 2013 - 03:19 AM

You should be able to use [your xml document varible name].GetElementsByTagName("goods") to get an xmlnodelist of of all the items in there and then you can just go through them all with nodelist[0], nodelist[1], etc, nodelist of course just being whatever name is chosen for the xmlnodelist variable. Pretty sure the xml stuff is still the same even though I use C# around it.

This post has been edited by .Aaron: 23 January 2013 - 03:21 AM

Was This Post Helpful? 1
  • +
  • -

#3 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 998
  • Joined: 30-July 08

Re: How to load new (unknown) items from a XML file.

Posted 23 January 2013 - 03:47 AM

I'll see if I can figure it out.

This is the function that read the XML document at the moment
void XMLParser::assign(char tag[], char name[])
{
	while(mMyXMLFile && mMyXMLFile->read())
	{
		switch(mMyXMLFile->getNodeType())
		{
			// irr::io::EXN_ELEMENT
			case irr::io::EXN_ELEMENT:
				if(!strcmp(tag, mMyXMLFile->getNodeName()))
				{
					// Check for the wanted tag and assign values to the variables.
					if(tag == "unit" || tag == "building")
					{
						std::string temp = mMyXMLFile->getAttributeValue("name");
						if(temp == name)
						{
							mName = mMyXMLFile->getAttributeValue("name");
							mType = mMyXMLFile->getAttributeValue("type");
							mTooltip = mMyXMLFile->getAttributeValue("tooltip");
							mPicture = mMyXMLFile->getAttributeValue("picture");
							mBuilt = mMyXMLFile->getAttributeValue("built");
							mTownID = mMyXMLFile->getAttributeValue("townID");
							mSoundFile = mMyXMLFile->getAttributeValue("sound");
						}
					}

					if(tag == "goods")
					{
						std::string temp = mMyXMLFile->getAttributeValue("name");
						if(temp == name)
						{
							mName = mMyXMLFile->getAttributeValue("name");
							mType = mMyXMLFile->getAttributeValue("type");
							mTooltip = mMyXMLFile->getAttributeValue("tooltip");
							mPicture = mMyXMLFile->getAttributeValue("picture");
							mValue = mMyXMLFile->getAttributeValue("value");
						}
					}
				}
			break;

			case irr::io::EXN_TEXT:
			break;

			default:
				break;
		}	
	}
}


goods.cpp
void Goods::load(char goods[])
{
	// What tag are we looking for, and what type of goods?
	xmlp.assign("goods", goods);
	
	//xp.assign("resource");
	mName = xmlp.getName();

	// convert from string to int.
	std::istringstream buffer(xmlp.getValue());

	// Put the value inside mBaseValue.
	buffer >> mBaseValue;
}

// This one I want to use so I don't have to say which I wanna load
// but I guess I have to rewrite here and at a few other places.
void Goods::load()
{
	xmlp.assign("goods");
	mName = xmlp.getName();
	std::istringstream buffer(xmlp.getValue());
	buffer >> mBaseValue;
}


TradeGoods.cpp
void TradeGoods::pushBack(char goods[])
{
	Goods *g = new Goods;
	g->load(goods); // I want to use g->load();
	mGoodsVector.push_back(g);
}



Note: I only added the "if(tag == "...")" in tradegoods, and the stuff in them. The other code in the same file is from the example. The pushBack function add the object into a vector<Goods*>, but if I use my

g->load(); function instead it will only load the last <goods ... /> as many times as I use the pushBack.

So now it load coal twice instead of food and coal.
This is the first time I have to do something like this so I am on deep water here. I have tried to find a solution, with no success.

This post has been edited by FrozenSnake: 23 January 2013 - 03:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3627
  • View blog
  • Posts: 11,319
  • Joined: 05-May 12

Re: How to load new (unknown) items from a XML file.

Posted 23 January 2013 - 10:15 AM

My recommendation is it use a real SAX parser, rather than that SAX-like XML parser that you are currently using from the Irrlicht framework. That way you can focus on just getting the data that you need, and getting it relatively cheaply and quickly. http://en.wikipedia....ple_API_for_XML

An alternative is to load in the DOM and doing searches as hinted by .Aaron.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1