Skip to main content
gamedev.net
🔒 Locked

TinyXML Parsing

Started by brcolow 6 replies 1.5k views
Original Post
brcolow
brcolow
I am trying to parse a file that looks like this:

<Conversation id="1"> 

<Line id="1">
<Content>FDAFASFAF</Content>
</Line>

</Conversation>

<Conversation id="2">

<Line id="1">
<Content>BLAHBLAHAH</Content>
</Line>

<Line id="2">
<Content>BLAHAHAHA</Content>
</Line>

</Conversation>
So I use this code:

	std::vector<CDialogueEntity*> temp;
	//Load xml file with param given in method
	TiXmlDocument doc(dialogueFile);
	loadXMLOkay = doc.LoadFile();

	//Check to see if XML file loaded correctly
	if (!loadXMLOkay)
	{
		gameManager->log << "Error: Dialogue file loading FAILED: " << dialogueFile << "\n";
	}
	else
	{
		gameManager->log << "Dialogue file loading succeeded: " << dialogueFile << "\n";
	}

	//Parse all conversations
	node = doc.FirstChild("Dialogue");
	for(TiXmlElement *diaAction = node->FirstChildElement("Conversation"); diaAction; diaAction = diaAction->NextSiblingElement()) 
	{
		CDialogueEntity* ent = new CDialogueEntity();

		//Acquire conversation ID
		attribute = diaAction->FirstAttribute();
		convoID = attribute->Value();

		//Show debug info
		gameManager->log << "\nDialogue Conversation ID: " << convoID << "\n";

		ent->setConvoID(atoi(convoID));

		for(TiXmlElement *diaAction2 = node->FirstChildElement("Line"); diaAction2; diaAction2 = diaAction2->NextSiblingElement()) 
		{
			//Acquire line ID
			attribute = diaAction2->FirstAttribute();
			lineID = attribute->Value();

			ent->setLineID(atoi(lineID));

			node = node->FirstChild("Content");
			text = node->FirstChild()->ToText();
			content = text->Value();

			contentString = content;

			ent->setLine(contentString);

			//Debug Info
			gameManager->log << "\nLine ID:" << lineID << "Content: " << content << "\n";

		}
			temp.push_back(ent);
			
			//Debug Info
			gameManager->log << "End of Conversation\n";
	}
But, that only prints the conversation stuff to the log...it is like it's skipping the second for loop. Any idea as to why? [Edited by - brcolow on February 21, 2005 5:24:37 PM]
-Brcolow-
Rob Loach
Rob Loach
You know about this right?
<Line id="1"><ContentBLAHBLAHAH</Content>  // OH TEH NOES!</Line>
Rob Loach [Website] [Projects] [
brcolow
brcolow
Whoops..I was taking out the dialogue for simplicity purposes..that isn't in the actual file..let me fix that.
-Brcolow-
Koshmaar
Koshmaar

Sorry I can't help you with your problem, but I'll give you little tip: don't parse everything manually, try writing (and using) little wrappers like those I did for my GUI parser:


//! From <_tagName> valueText </_tagName> returns valueTextconst char * GetTagValueTxt(const TiXmlElement * _xmlStream, const char * _tagName);  // ------------------------------------------------- start GetTagValueTxtconst char * SC :: GetTagValueTxt(const TiXmlElement * _xmlStream, const char * _tagName) {  TiXmlElement * tag = _xmlStream->FirstChildElement(_tagName);    if (tag)   {    TiXmlNode * node = tag->FirstChild();          if (node)     {      TiXmlText * value = node->ToText();      if (value) return (value->Value());       else return 0;     } else return 0;    } else return 0;      }    // ------------------------------------------------- end GetTagValueTxt


  // slint == signed long int;  //! From <_tagName> valueSlint </_tagName> returns converted valueSlint  slint GetTagValueSlint(const TiXmlElement * _xmlStream, const char * _tagName);// ------------------------------------------------- start GetTagValueSlintSC :: slint SC :: GetTagValueSlint(const TiXmlElement * _xmlStream, const char * _tagName)  {  TiXmlElement * tag = _xmlStream->FirstChildElement(_tagName);              if (tag)   {    TiXmlNode * node = tag->FirstChild();          if (node)     {      TiXmlText * value = node->ToText();      if (value) return atoi(value->Value());       else return 0;     } else return 0;    } else return 0;         }     // ------------------------------------------------- end GetTagValueSlint



I've been working on GUI and it didn't work, but parsing code was one big mess... so I wrote those wrappers (along with ParseFontTag which I won't paste here since it's too much tied down to my own engine), used them... and it worked! :-)
brcolow
brcolow
The problem still exists though...it seems like the for loop is setup wrong. But how?
-Brcolow-
brcolow
brcolow
Hmm..I seem to be pretty stuck with this. Any other ideas?

Thanks.
-Brcolow-
Assassin
Assassin
for(TiXmlElement *diaAction2 = node->FirstChildElement("Line")

you're gonna want to call that on diaAction instead of node
Assassin, aka RedBeard. andyc.org
brcolow
brcolow
Wow thanks so much Assassin, worked perfectly. Your a life saver :).
-Brcolow-

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.