PDA

View Full Version : Qt extract from xml with DOM



Leoha_Zveri
4th October 2009, 11:59
ok, so I have a xml file... smth like this:


<?xml version = "1.0" ?>
<!-- My Bookmarks -->
<db>
<user nr="1">
<username>Leoha_Zveri</username>
<password>testpass</password>
</user>
</db>

in my code i have a function:


bool DOMtraverseNode(const QDomNode& domNode) {
QDomNode node = domNode.firstChild();
while( !node.isNull() )
{
if ( node.isElement() )
{
QDomElement element = node.toElement();
qDebug() << "Element: " << element.tagName();
qDebug() << "Element attribute name: " << element.attribute("nr", "not set");

}

if (node.isText())
{
QDomText text = node.toText();
qDebug() << text.data();
}


DOMtraverseNode(node);
node = node.nextSibling();
}
}

and in main:


QFile file("admin.xml");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug("Failed to open file for reading.");
return -1;
}

QDomDocument document;
if (!document.setContent(&file))
{
qDebug("Failed to parse the file into a DOM tree.");
file.close();
return -1;
}

QDomElement domNode = document.documentElement();
DOMtraverseNode(domNode);


file.close();

the question is... how do I check if a 2 QStrings (for example "uname" and "pass" ) entered by the user match, the values from the xml? I can check them separately, returning something from function when it matches the username, and another time when it matches the password,... but what if the xml looks like this:


<?xml version = "1.0" ?>
<!-- My Bookmarks -->
<db>
<user nr="1">
<username>Leoha_Zveri</username>
<password>testpass2</password>
</user>
<user nr="2">
<username>Leoha_Zveri2</username>
<password>testpass</password>
</user>
</db>

... functions will return that both values are contained in xml, and it will allow to log in, but it's wrong...
so how can i make this work?
sorry for my bad english...
please someone help