PDA

View Full Version : Convert XML to QTreeView



sergio87
27th September 2011, 14:45
Hi all,

I read a very simple XML created by me. I parse it on a QDomDocument.

My XML is:


<?xml version='1.0' encoding='utf-8'?>
<data>
<feature>
<name>Sex</name>
<value>Male</value>
<value>Female</value>
<value>Unknown</value>
</feature>
<feature>
<name>Color</name>
<value>Red</value>
<value>Green</value>
<value>Blue</value>
<value>White</value>
<value>Black</value>
<value>Unknown</value>
</feature>
<feature>
<name>Size</name>
<value>Small</value>
<value>Medium</value>
<value>Large</value>
<value>Unknown</value>
</feature>

</data>


I parse the XML:

QDomDocument doc("mydocument");
QFile file("metadata.xml");
if (!file.open(QIODevice::ReadOnly)){
cout<< "ERROR"<< endl;
return;
}
if (!doc.setContent(&file)) {
cout << "ERROR" << endl;
file.close();
return;
}
file.close();


So, I want to show the XML in a QTreeView.To do that, I have to create a QStandardItemModel. ¿Any ideas?

My idea is something like this:

void MainWindow::preOrder(QDomNode dom, QStandardItemModel *model, int row){
if(!dom.isNull()){
int nRow = row+1;
string aux = dom.nodeName().toStdString();
string name = "name";
string value = "value";
if(dom.isText()){
aux = dom.parentNode().nodeName().toStdString();
if(strcmp(aux.c_str(),name.c_str())==0){
cout << dom.nodeValue().toStdString() << endl;
//New ROW
SubQStandardItem *item = new SubQStandardItem(dom.nodeValue());
item->setEditable(false);
model->setItem(nRow, item);
}
if(strcmp(aux.c_str(),value.c_str())==0){
//New SubItems
cout << "\t" << dom.nodeValue().toStdString() << endl;
SubQStandardItem *subItem = new SubQStandardItem( dom.nodeValue() );
subItem->setCheckable(true);
subItem->setEditable(false);
}
}else{
preOrder(dom.firstChild(), model, nRow);
preOrder(dom.nextSibling(), model, nRow);
}

}
}

This code (not work...) is based on the pre-order algorithm.

Please help!!! :o

Thanks in advance!!!! :D

sergio87
28th September 2011, 09:00
Hi all,

I solved it! :p

My solution is very simple and it works well for my xml. The code is:



void MainWindow::startTree(){
QDomDocument doc("mydocument");
QFile file("metadata.xml");
if (!file.open(QIODevice::ReadOnly)){
cout<< "ERROR"<< endl;
return;
}
if (!doc.setContent(&file)) {
cout << "ERROR" << endl;
file.close();
return;
}
file.close();
QStandardItemModel *model = new QStandardItemModel();
preOrder(doc.firstChild(), model);
view = new QTreeView(centralWidget);
view->setModel(model);
view->show();

}

void MainWindow::insertFather(QString name){
item = new QStandardItem(name);
item->setEditable(false);
}

void MainWindow::insertChildren(QString name){
QStandardItem *subItem = new QStandardItem( name );
subItem->setCheckable(true);
subItem->setEditable(false);
item->appendRow(subItem);
}

void MainWindow::setItem(QStandardItemModel *model){
model->setItem(nRow, item);
nRow++;
}

void MainWindow::preOrder(QDomNode dom, QStandardItemModel *model){
if(!dom.isNull()){
string aux = dom.nodeName().toStdString();
string name = "name";
string value = "value";
if(dom.isText()){
aux = dom.parentNode().nodeName().toStdString();
if(strcmp(aux.c_str(),name.c_str())==0){
cout << dom.nodeValue().toStdString() << endl;
insertFather(dom.nodeValue());
setItem(model);
}
if(strcmp(aux.c_str(),value.c_str())==0){
cout << "\t" << dom.nodeValue().toStdString() << endl;
insertChildren(dom.nodeValue());
}
}else{
preOrder(dom.firstChild(), model);
preOrder(dom.nextSibling(), model);
}

}
}


Where item and nRow are declared at header as private.

Hope it help you!! ;)

Regards!