Problems reading XML with QDomDocument
Hi,
I am trying to read configuration with QDomDocument. I managed to get writting configuration to work. When reading I get the settings but don't get any phone numbers.
This is an example of what I might read (there can be between 0 and 999 phone numbers):
Code:
<!--Main Settings-->
<settings AfterHourOffset="100" NumPerButton="1" MaxNumToDial="1" />
<!--Phone Book List-->
<phone location="0" value="911" />
<phone location="1" value="33782134" />
<phone location="2" value="000" />
<phone location="3" value="ABCD" />
<phone location="8" value="13005696" />
<phone location="990" value="38441104" />
<phone location="999" value="131333" />
Here's my current attempt:
Code:
d.setContent(&file);
ClearPhoneNumbers(); // Clear phone table.
while(!n.isNull()) {
if(n.isElement()) {
if(name == "settings") {
ui->Edit1->setText(e.attribute("AfterHourOffset",""));
ui->Edit2->setText(e.attribute("MaxNumToDial",""));
ui->Edit3->setText(e.attribute("NumPerButton",""));
} else if(name == "phone") {
QString location
= e.
attribute("location",
"");
QString value
= e.
attribute("value",
"");
AddPhoneBook(location, value); // Add to phone table.
}
}
n = n.nextSibling();
}
file.close();
}
See whats wrong? :confused:
Thanks
Brendan
Re: Problems reading XML with QDomDocument
your xml format is not corrected. (you xml doesn't contain root element).
you should have xml like this
Code:
<phones>
<!--Main Settings-->
<settings AfterHourOffset="100" NumPerButton="1" MaxNumToDial="1" />
<!--Phone Book List-->
<phone location="0" value="911" />
<phone location="1" value="33782134" />
<phone location="2" value="000" />
<phone location="3" value="ABCD" />
<phone location="8" value="13005696" />
<phone location="990" value="38441104" />
<phone location="999" value="131333" />
</phones>
Re: Problems reading XML with QDomDocument
I think you should use something as:
Quote:
<?xml version='1.0' encoding='windows-1251'?>
<root>
<data1></data1>
....
</root>
Re: Problems reading XML with QDomDocument
Thanks for replies,
I tried both suggestions. Now it only collects the root element. Then after NextSibling is called it breaks out of the while loop. So nothing is collected. I must be missing something.
Re: Problems reading XML with QDomDocument
I use this code to read xml.
Code:
bool CSKHttp::GetDataFromXml(CSKRequest* req)
{
int errorLine;
int errorColumn;
req->m_buffer.close();
if(!(domDoc.setContent(byteArray,false,&errorString,&errorLine,&errorColumn))) {
mocW("rq")<<"xml document error <MESS:"<<errorString<<"><LINE"
<<errorLine<<"><COLUMN"<<errorColumn<<">"<<endl;
mocW("rq")<<"<ANSWER:"<<byteArray<<">"<<endl;
return false;
}
byteArray.replace("\n"," ");
mocD("rq")<<"<ANSWER:"<<byteArray<<">"<<endl;
TraverseXmlNode(domElement,req);
return true;
}
void CSKHttp::TraverseXmlNode(const QDomNode& node, CSKRequest* req)
{
while(!(domNode.isNull())) {
if(domNode.isElement()) {
domElement = domNode.toElement();
if(!(domElement.isNull()))
req->Data(domElement.tagName(),domElement.text());
}
TraverseXmlNode(domNode,req);
domNode = domNode.nextSibling();
}
}
may be useful for you.
Re: Problems reading XML with QDomDocument
You get the first child, which would be "phones". Then you try to get its siblings but it doesn't have any. You need to get the children of phones. So you can do:
Code:
d.setContent(&file);
if(!n.isNull() || !n.hasChildNodes())
return;
n = n.firstChild();
ClearPhoneNumbers(); // Clear phone table.
while(!n.isNull()) {
...
Re: Problems reading XML with QDomDocument
I wrote this in a browser, but I'm sure that this code works fine. ;)
Code:
QFile file("phones.xml");
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();
while(!n.isNull()) {
if(!e.isNull()) {
if (e.tagName() == "phone") {
qDebug() << "location" << e.attribute("location");
qDebug() << "value" << e.attribute("value");
} else if (e.tagName() == "settings") {
qDebug() << "AfterHourOffset" << e.attribute("AfterHourOffset");
qDebug() << "NumPerButton" << e.attribute("NumPerButton");
qDebug() << "MaxNumToDial" << e.attribute("MaxNumToDial");
}
}
n = n.nextSibling();
}
Re: Problems reading XML with QDomDocument
Thanks poeple. ;)
Spirits version worked for me.
I ended up making mine a combination of Spirit and victor.fernandez ideas.
I'm a bit scared to do recursive programming unless its required.
Re: Problems reading XML with QDomDocument
Quote:
Originally Posted by
grantbj74
Thanks poeple. ;)
....
I'm a bit scared to do recursive programming unless its required.
don`t be afraid it`s realy easy.
Re: Problems reading XML with QDomDocument
Quote:
I'm a bit scared to do recursive programming unless its required.
There is some risk of making a mistake and entering an infinite recursion but no more than there is with a plain for(). Being a bit careful, you should not be afraid of it. Also keep in mind that by traversing a QDomDocument you won't have any problem because it's a plain tree without loop backs.