parsing xml files with tinyxml

Started by
8 comments, last by WavyVirus 13 years ago
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.
Advertisement
your XML is missing

<?xml version="1.0"?>


at the top. Or did you simply not copy/paste that line?
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?
Try:

TiXmlElement* root = docHandle.FirstChildElement( "TodoList" ).Element();


then you can grab the items via:

root->FirstChildElement("item").Element()

Admin for GameDev.net.

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)
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).
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.
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.
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.

Omae Wa Mou Shindeiru

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)

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();
}

This topic is closed to new replies.

Advertisement