Running on: Linux Mint 16, QtCreator 3.0.0 with Qt 5.2.0. tinyXML last version.
XML working with:
<users>
<user>
<username>testadmin</username>
<password>testpwd</password>
<privileges>2</privileges>
</user>
<user>
<username>testuser</username>
<password>testpwd</password>
<privileges>1</privileges>
</user>
Code that's not working:
void Server::loadUsersFile()
{
TiXmlDocument usersDoc("users.xml");
bool loadOk = usersDoc.LoadFile();
if(!loadOk) {
cout << "Error opening users file" << endl;
return;
}
TiXmlElement* rootChild = usersDoc.FirstChildElement("users");
TiXmlElement* userChild = rootChild->FirstChildElement("user");
TiXmlAttribute* pAttrib=userChild->FirstAttribute();
cout << pAttrib->Name();
}
pAttrib is NULL and I can't understand why. Maybe I didn't get the Child/Attribute relationship. Help appreciated.
Your XML doesn't have any attributes. It has text within elements, but no attributes.
If your XML had something like:
<user name="foo" />
then it would give you an attribute. And you could have multiple attributes of course:
<user username="testadmin" password="testpassword" privileges="2" />
You could either change your code, or change your XML to use attributes. If you do, I'd recommend that you ask for specific attributes rather than requiring that the username is the first attribute, for example.
If you're going to work with XML, it's important to know the terminology, otherwise you'll get very confused. It might be worth looking through an XML tutorial before going much further.
See more on this question at Stackoverflow