XML files

I need to extract information from an XML file for use in a c++ program. My first look around for a solution directed me to http://xerces.apache.org/xerces-c/. I was able to build and install xerces but I was unable to discover how to use it for my needs.

If anyone has another solution or anyone familiar with xerces that could give me some tips or point me to a comprehensive tutorial I would greatly appreciate it.
TinyXML is much simpler and has some good howto/tutorials.
I have tried rapidXML and TinyXML as well. I don't know what I am doing wrong but I am unable to get them to work. Do you have an example source file with an example xml file I can try to compile on my system to see if they work?
While I appreciate that you've found the tutorial, I was able to find that tutorial myself. I had trouble compiling anything based on the examples. I will include my code in my next post and maybe someone can tell me what little mistake I've made.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include"tinyxml.cpp"
#include"tinyxml.h"
#include"tinyxmlerror.cpp"
#include"tinyxmlparser.cpp"
#include"tinystr.cpp"
#include"tinystr.h"

using namespace std;

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

int main(void){
	dump_to_stdout("example1.xml");
	return 0;	
}


I get the following error when compiling

1
2
3
4
>g++ tinyxmltest.cpp -o tinyxmltest.out
tinyxmltest.cpp: In function ‘void dump_to_stdout(const char*)’:
tinyxmltest.cpp:17: error: cannot convert ‘TiXmlDocument*’ to ‘const char*’ for 
argument ‘1’ to ‘void dump_to_stdout(const char*)’
Last edited on
The problem is that the argument to dump_to_stdout() should be a reference to a TiXmlDocument, not a cstring constant. This part of the tutorial seems to be in a bit ambiguous and overall there seem to be some significant typos! I feel sorry for pointing you there now... I fixed it up here though:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "tinyxml.h"
#include "tinystr.h"
#include "tinyxml.cpp"
#include "tinystr.cpp"
#include "tinyxmlerror.cpp"
#include "tinyxmlparser.cpp"

using namespace std;

const unsigned int NUM_INDENTS_PER_SPACE=2;

const char * getIndent( unsigned int numIndents )
{
	static const char * pINDENT="                                      + ";
	static const unsigned int LENGTH=strlen( pINDENT );
	unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
	if ( n > LENGTH ) n = LENGTH;

	return &pINDENT[ LENGTH-n ];
}

// same as getIndent but no "+" at the end
const char * getIndentAlt( unsigned int numIndents )
{
	static const char * pINDENT="                                        ";
	static const unsigned int LENGTH=strlen( pINDENT );
	unsigned int n=numIndents*NUM_INDENTS_PER_SPACE;
	if ( n > LENGTH ) n = LENGTH;

	return &pINDENT[ LENGTH-n ];
}

int dump_attribs_to_stdout(TiXmlElement* pElement, unsigned int indent)
{
	if ( !pElement ) return 0;

	TiXmlAttribute* pAttrib=pElement->FirstAttribute();
	int i=0;
	int ival;
	double dval;
	const char* pIndent=getIndent(indent);
	printf("\n");
	while (pAttrib)
	{
		printf( "%s%s: value=[%s]", pIndent, pAttrib->Name(), pAttrib->Value());

		if (pAttrib->QueryIntValue(&ival)==TIXML_SUCCESS)    printf( " int=%d", ival);
		if (pAttrib->QueryDoubleValue(&dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
		printf( "\n" );
		i++;
		pAttrib=pAttrib->Next();
	}
	return i;
}

void dump_to_stdout( TiXmlNode* pParent, unsigned int indent = 0 )
{
	if ( !pParent ) return;

	TiXmlNode* pChild;
	TiXmlText* pText;
	int t = pParent->Type();
	printf( "%s", getIndent(indent));
	int num;

	switch ( t )
	{
	case TiXmlNode::TINYXML_DOCUMENT:
		printf( "Document" );
		break;

	case TiXmlNode::TINYXML_ELEMENT:
		printf( "Element [%s]", pParent->Value() );
		num=dump_attribs_to_stdout(pParent->ToElement(), indent+1);
		switch(num)
		{
			case 0:  printf( " (No attributes)"); break;
			case 1:  printf( "%s1 attribute", getIndentAlt(indent)); break;
			default: printf( "%s%d attributes", getIndentAlt(indent), num); break;
		}
		break;

	case TiXmlNode::TINYXML_COMMENT:
		printf( "Comment: [%s]", pParent->Value());
		break;

	case TiXmlNode::TINYXML_UNKNOWN:
		printf( "Unknown" );
		break;

	case TiXmlNode::TINYXML_TEXT:
		pText = pParent->ToText();
		printf( "Text: [%s]", pText->Value() );
		break;

	case TiXmlNode::TINYXML_DECLARATION:
		printf( "Declaration" );
		break;
	default:
		break;
	}
	printf( "\n" );
	for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
	{
		dump_to_stdout( pChild, indent+1 );
	}
}

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

int main()
{
	dump_to_stdout("example1.xml");
	return 0;
}

closed account (o3hC5Di1)
Hi there,

Quite ambiguous that they're using dump_to_stdout() twice as a name with different parameter lists.

The tutorials declares it as follows in the tinyxml.h file :
void dump_to_stdout( TiXmlNode* pParent, unsigned int indent = 0 )

So for some reason your line 17 is not picking up on that function taking a TiXmlNode* as a parameter and tries to use the function recursively.

Why this is, I'm afraid I have no clue, hopefully one of the experts around here is kind enough to shed their light on it.

Perhaps you can make sure if that function is in fact defined in the tinyxml.h file, that would be my only guess.

Hope you get this figured out.

All the best,
NwN

Thanks Texan40. Any chance you can throw in some comments here and there to help me better understand what it is that is going on? Hopefully then I can use this to extract data from an xml file.
Topic archived. No new replies allowed.