PDA

View Full Version : QtXml questions



Fastman
2nd February 2008, 15:30
I need to realize a class for parsing configuration files with following features:

(1): GetNodeValue(QString cnode, QString &cvaluenode, int count) const

For example


XmlParser pXmlParser;

if(!pXmlParser.OpenXml(cCmd))
return false;

QString cTmp;
QString cValue;
QString cXml;

cXml = "node1/node2/node3"; // <- Thus I need to specify root

cTmp = cXml + "/brw"; // <- specify Node
pXmlParser.GetNodeValue(cTmp, cValue); // <- get value this node
m_psInfoUser.bOnBrowse = cValue == "1" ? true : false;


(2) GetNodeList(QString cnode, StrVector &nodes) const
(3) GetNodeNameList(QString cnode, StrVector &nodes) const
(4) GetAttrList(QString cnode, QString nameattr, StrVector &attr) const
(5) GetAttrValue(QString cnode, QString nameattr, QString& valattr, int count) const

What advise to esteem, and there are some ready examples which it can is possible to take for a basis?

wysota
2nd February 2008, 16:04
I think you should use the DOM approach - see QDomDocument and friends. Using Patternist (available in 4.4 snapshots) is an option as well.

Fastman
3rd February 2008, 12:48
Hmm.... Now I study documents on QDomDocument. But has not found how I knowing a way to my element I can receive its value.
For example I have structure:


<?xml version="1.0" encoding="windows-1251"?>
<root>
<client>
<prof>
<brw>1</brw>
<srch>1</srch>
<reg>1</reg>
<edm>1</edm>
<edd>1</edd>
<mgn>1</mgn>
</prof>
</client>
</root>

I can directly on a way of an element take value or me will have to bypass all elements searching the for the ?

wysota
3rd February 2008, 22:36
QDomDocument doc;
doc.setContent(...);
//...
QDomElement root = doc.documentElement();
QDomElement srch = root.firstChildElement("client").firstChildElement("prof").firstChildElement("srch");
QString s = srch.text(); // s=="1"

Fastman
4th February 2008, 09:00
QDomDocument doc;
doc.setContent(...);
//...
QDomElement root = doc.documentElement();
QDomElement srch = root.firstChildElement("client").firstChildElement("prof").firstChildElement("srch");
QString s = srch.text(); // s=="1"

Thx Wysota !!! :)
my code:


bool XmlParser::GetNodeValue(QString cnode, QString &cvaluenode, qint16 count)
{
QStringList listNodes = cnode.split("/");

QDomElement root = doc->documentElement();
QDomElement srch = root.firstChildElement(listNodes.at(1));

for (qint8 i = 2; i < listNodes.size(); i++)
{
srch = srch.firstChildElement(listNodes.at(i));
}

cvaluenode = srch.text();

return true;
}




...
cXml = "root/client/prof";

cTmp = cXml + "/brw";
pXmlParser.GetNodeValue(cTmp, cValue);
m_psInfoUser.bOnBrowse = cValue == "1" ? true : false;
...