[Qt-interest] create a tree from xml

qingzhi yan yanqzhi at gmail.com
Thu Jan 28 16:29:03 CET 2010


hi,

I want to create a tree from a xml file,but it does not work.
the code :

void ParseXml::on_pushButton_2_clicked()
{
QFile file("testXml.xml");
if(!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(0, "", "Open file failed");
return;
}

QDomDocument dom;
if (!dom.setContent(&file))
{
QMessageBox::warning(0, "", "DomDocument setContent failed");
return;
}
QDomElement e = dom.firstChildElement().firstChildElement("folder");
int itemCount = 0;
while (!e.isNull())
{
QTreeWidgetItem * item;
QStringList str ;
if (!e.firstChildElement().isNull())
{
str << e.tagName() << e.attribute("name");
item = new QTreeWidgetItem( str );//QStringList(e.attribute("name") )
ui.treeWidget->insertTopLevelItem(itemCount, item);
parseElement(e, item);
}
else
{
str << e.tagName() << e.text();
item = new QTreeWidgetItem(QStringList(e.text() ) );
ui.treeWidget->insertTopLevelItem(itemCount, item);
}
itemCount ++;

e =e.nextSibling().toElement();
}
}

void ParseXml::parseElement(QDomElement e, QTreeWidgetItem * parentItem)
{
QDomElement element = e.firstChildElement();
if (element.isNull())
{
return;
}
QStringList str ;
if (!element.firstChildElement().isNull())
{
str << element.tagName() << element.attribute("name");
QTreeWidgetItem * item = new QTreeWidgetItem(parentItem,
str);//QStringList(e.attribute("name") )
parseElement(element, item);
}
else
{
str << element.tagName() << element.text();
QTreeWidgetItem * item = new QTreeWidgetItem(parentItem,
QStringList(element.text() ) );
}

element =element.nextSiblingElement();
parseElement(element, parentItem);
}

How can I make the tree like below:
  aa
<title>Literate Programming</title>
<bookmark name="http://www.vivtek.com/litprog.html">
<title>Synopsis of Literate Programming</title>
</bookmark>
<bookmark name="
http://vasc.ri.cmu.edu/old_help/Programming/Literate/literate.html">
<title>Literate Programming: Propaganda and Tools</title>
<title>Literate Programming by Henrik Turbell</title>
</bookmark>


Thanks a lot
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100128/be9bacc5/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testXml.xml
Type: text/xml
Size: 1198 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100128/be9bacc5/attachment.xml 


More information about the Qt-interest-old mailing list