• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
scarypajamas

parsing xml files with tinyxml

9 posts in this topic

I’m trying to parse an xml file with tinyxml. I’ve gone over the api and tutorials, but my own implementation isn’t working. My xml looks like this:
<TodoList>
	<item group="group1" title="script">
		<date year="2006" month="04" day="17"/>
		<text>make some script
		</text>
	</item>

	<item group="misc" title="tinyxml">
		<date year="2006" month="04" day="23"/>
		<text>fix it
		</text>
	</item>

</TodoList>




I’ve tried two methods but neither of them are working. Neither of them recognize <TodoList> and if I could get that, I think I could take it form there. My First try:
void to_stdout( TiXmlNode* pParent )
{
	if ( !pParent )
		return;

	TiXmlNode* root = pParent->FirstChildElement( "TodoList" );
	if ( root )
	{
		printf( "HELLO WORLD!\n" );
		
		TiXmlNode* element = root->FirstChildElement( "group1" );
		if ( element )
		{
			printf( "FOUND YOU!\n" );
		
			TiXmlNode* child = element->FirstChildElement( "Child" );
			if ( child )
			{
				TiXmlNode* child2 = child->NextSiblingElement( "Child" );
				if ( child2 )
				{
					// Finally do something useful.
				}
			}
		}
	}
}

// load the named file and dump its structure to STDOUT
void dump_to_stdout(const char* pFilename)
{
	TiXmlDocument doc(pFilename);
	bool loadOkay = doc.LoadFile();
	if (loadOkay)
	{
		printf("\n%s:\n", pFilename);
		to_stdout( &doc ); // defined later in the tutorial
	}
	else
	{
		printf("Failed to load file \"%s\"\n", pFilename);
	}
}




Second try:
void to_stdout( TiXmlHandle docHandle )
{
	//if ( !docHandle )
	//	return;

	TiXmlElement* root = docHandle.FirstChild( "TodoList" ).Element();
	if ( root )
	{
		printf( "HELLO WORLD!\n" );
	
		TiXmlNode* element = root->FirstChildElement( "group1" );
		if ( element )
		{
			printf( "FOUND YOU!\n" );
		
			TiXmlNode* child = element->FirstChildElement( "Child" );
			if ( child )
			{
				TiXmlNode* child2 = child->NextSiblingElement( "Child" );
				if ( child2 )
				{
					// Finally do something useful.
				}
			}
		}
	}
}

// load the named file and dump its structure to STDOUT
void dump_to_stdout(const char* pFilename)
{
	TiXmlDocument doc(pFilename);
	bool loadOkay = doc.LoadFile();
	if (loadOkay)
	{
		printf("\n%s:\n", pFilename);
		TiXmlHandle docHandle( &doc );
		
		to_stdout( docHandle ); // defined later in the tutorial
	}
	else
	{
		printf("Failed to load file \"%s\"\n", pFilename);
	}
}
 



If anyone could shed some light on this, that would be great.
0

Share this post


Link to post
Share on other sites
Ok, I included the line at the top of my xml file but my code is still not returning anything.

btw, I'm using a Mac (xcode as the IDE) and I included all the tinyxml header and source files in my project. Isn't that all I'm supposed to do to make it work?
0

Share this post


Link to post
Share on other sites
Ok, I think my version of tinyXml is bad or something, because I’m running the dump_to_stdout function that comes with the tutorial in the documentation. It’s supposed to be a cut and past function but my results are coming out totally different then the results posted in the tutorial. The “Elements” part specifically are outputting weird.

I’m using version 2.5.3 of tinyxml and I’m on a Mac using xcode. I added all the tinyxml source files according to the readme to my project and it compiles fine, but I’m having trouble even with the tutorials and copying and pasting the function, to get the same results as in the tutorial. Does anyone know why tinyxml hates me?

Here’s the xml code form the tutorial:


<?xml version="1.0" ?>
<MyApp>
<!-- Settings for MyApp -->
<Messages>
<Welcome>Welcome to MyApp</Welcome>
<Farewell>Thank you for using MyApp</Farewell>
</Messages>
<Windows>
<Window name="MainFrame" x="5" y="15" w="400" h="250" />
</Windows>
<Connection ip="192.168.0.1" timeout="123.456000" />
</MyApp>



Here’s the output generated with the dump_to_stdout function shown in the documentation.


Document
+ Declaration
+ Element [MyApp]
(No attributes)
+ Comment: [ Settings for MyApp ]
+ Element [Messages]
(No attributes)
+ Element [Welcome]
(No attributes)
+ Text: [Welcome to MyApp]
+ Element [Farewell]
(No attributes)
+ Text: [Thank you for using MyApp]
+ Element [Windows]
(No attributes)
+ Element [Window]
+ name: value=[MainFrame]
+ x: value=[5] int=5 d=5.0
+ y: value=[15] int=15 d=15.0
+ w: value=[400] int=400 d=400.0
+ h: value=[250] int=250 d=250.0
5 attributes
+ Element [Connection]
+ ip: value=[192.168.0.1] int=192 d=192.2
+ timeout: value=[123.456000] int=123 d=123.5
2 attributes




My output, when I copy and pasted the function and tried to run it (it’s meant to be copy and pasted btw) is different. It’s about 6 pages longer (I only posted a page just to give you the idea) and the values are different compared to the original output in the tutorial shown above. But why is it different? It should be the same since it’s the same function! Does anyone know why tinyXml hates me?


Document
+ Declaration
+ Unknown
+ Element [w:wordDocument]
+ xmlns:w: value=[http://schemas.microsoft.com/office/word/2003/wordml]
+ xmlns:v: value=[urn:schemas-microsoft-com:vml]
+ xmlns:w10: value=[urn:schemas-microsoft-com:office:word]
+ xmlns:sl: value=[http://schemas.microsoft.com/schemaLibrary/2003/core]
+ xmlns:aml: value=[http://schemas.microsoft.com/aml/2001/core]
+ xmlns:wx: value=[http://schemas.microsoft.com/office/word/2003/auxHint]
+ xmlns:o: value=[urn:schemas-microsoft-com:office:office]
+ xmlns:dt: value=[uuid:C2F41010-65B3-11d1-A29F-00AA00C14882]
+ w:macrosPresent: value=[no]
+ w:embeddedObjPresent: value=[no]
+ w:ocxPresent: value=[no]
+ xml:space: value=[preserve]
12 attributes
+ Element [o:DocumentProperties]
(No attributes)
+ Element [w:fonts]
(No attributes)
+ Element [w:defaultFonts]
+ w:ascii: value=[Times New Roman]
+ w:fareast: value=[Times New Roman]
+ w:h-ansi: value=[Times New Roman]
+ w:cs: value=[Times New Roman]
4 attributes
+ Element [w:docPr]
(No attributes)
+ Text: [w:defaultTabStop w:val="720"/>]
+ Element [w:body]
(No attributes)
+ Element [wx:sect]
(No attributes)


0

Share this post


Link to post
Share on other sites
Looks like you're not reading from the same file.

Are you editing the xml file in Microsoft Word? because it's possible that Word changes the xml file somehow. (Or at least that's what it looks like to me).
0

Share this post


Link to post
Share on other sites
Quote:
Are you editing the xml file in Microsoft Word? because it's possible that Word changes the xml file somehow. (Or at least that's what it looks like to me).


[grin] I can’t believe it. That was it! I’ve been creating xml files by renaming the .txt to .xml and I didn’t think it was screwing it up but it was. Thank you sprite_hound it prints out fine now.
0

Share this post


Link to post
Share on other sites
I am trying to get the "id" and the "ref" values from this xml file using tinyxml in c++ but I am not being able to get the all the "ref" values . Can anyone help please?

<?xml version="1.0" encoding="UTF-8"?>


<way id="40619942" user="Alexandre Chabran" uid="157521" visible="true" version="1" changeset="2445846" timestamp="2009-09-11T13:01:18Z">
<nd ref="493347121"/>
<nd ref="493347151"/>
<nd ref="493347152"/>
<nd ref="493347138"/>
<nd ref="493347139"/>
<nd ref="493347140"/>
<nd ref="493347141"/>
<nd ref="493347121"/>
<tag k="building" v="yes"/>
<tag k="source" v="cadastre-dgi-fr source : Direction Générale des Impôts - Cadastre ; mise à jour : 2009"/>
</way>


Thanks.
0

Share this post


Link to post
Share on other sites
What do you mean by "getting the ref values"? They are plain old attributes, no more difficult to access than the "id" attribute of the "way" element.
Are you treating them as refid? Then they are obviously dangling, refid attributes work within a single document. Or are you somehow failing to iterate over the sequence of "nd" elements?
You should post code and explain the intended and wrong outcomes.

PS: don't resurrect old threads like this. Demonstrating ability to search the forum is good, but your question is obviously unrelated and it should be in its own thread.
1

Share this post


Link to post
Share on other sites
Is this OpenStreetMap data? Coincidentally I wrote an app recently which had to parse this and recognized the format.

This should get you started (WARNING! There is no error checking - we are assuming the XML is properly formed and matches the expected schema. You should check the return values of LoadFile and QueryIntAttribute)

[code]TiXmlDocument doc("map.xml");
doc.LoadFile();

TiXmlElement *currentElement = doc.RootElement()->FirstChildElement();

while (currentElement != null)
{
string elementType = currentElement->ValueStr();

if (elementType == "way")
{
int wayId;
currentElement->QueryIntAttribute("id", &wayId);

cout << "Way ID: " << wayID << endl;

TiXmlElement *wayChildElement = currentElement->FirstChildElement();

while (wayChildElement != null)
{
if (wayChildElement->ValueStr() == "nd")
{
int ndRef;
wayChildElement->QueryIntAttribute("ref", &ndRef);
cout << " ND ref: " << ndRef << endl;
}

wayChildElement = wayChildElement->NextSiblingElement();
}
}

currentElement = currentElement->NextSiblingElement();
}
[/code]
0

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  
Followers 0