Parse XML file



  • Hellow!
    I have XML file:

    @<?xml version="1.0" encoding="UTF-8"?>
    <data>
    <item>C:/Qt</item>
    <folder name="Friends">
    <item>C:/Qt1</item>
    <item>C:/Qt2</item>
    </folder>
    </data>@

    I want to parse it, and add items and folders in my QTreeView
    I want to do it:
    I call this function:
    @RizekFaster::RizekFaster(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    setupUi(this);

    QDomDocument domDoc;
    QFile file(":/RizekFaster/data.xml");

    itemModel = new QStandardItemModel(0,1);
    dataTreeView->setModel(itemModel);
    if(file.open(QIODevice::ReadOnly))
    {
    if(domDoc.setContent(&file))
    {
    QDomElement domElement = domDoc.documentElement();
    traverseNode(domElement);
    }
    file.close();
    }
    }@
    @void RizekFaster::traverseNode(const QDomNode& node, QModelIndex& index, int count)
    {
    QDomNode domNode = node.firstChild();
    while (!domNode.isNull())
    {
    if(domNode.isElement())
    {
    QDomElement domElement = domNode.toElement();
    if(!domElement.isNull())
    {
    if(domElement.tagName() == "folder")
    {
    itemModel->insertRow(count,index);
    itemModel->setData(itemModel->index(count,0,index),domElement.attribute("name"));
    traverseNode(domNode,itemModel->index(count+1,0,index));
    }
    if(domElement.tagName() == "item")
    {
    if(index.isValid())
    {
    itemModel->insertRow(count,index);
    }
    else
    {
    itemModel->insertRow(count);
    }
    itemModel->setData(itemModel->index(count,0,index),domElement.text());
    }
    }
    }
    count++;
    domNode = domNode.nextSibling();
    //traverseNode(domNode);
    }
    }@

    Function parse this file in this:
    !http://s1.hostingkartinok.com/uploads/images/2012/01/6293858b45cc57efc1adddc68e2bcf87.jpg(this)!



  • And what's wrong with it?





  • I think that this man got a tutorial about it in video format.
    http://www.voidrealms.com/

    I liked this tutorials in the beginning of my QT programming.



  • Hi,

    I think the bug is located here:

    @
    if(domElement.tagName() == "folder")
    {
    itemModel->insertRow(count,index);
    itemModel->setData(itemModel->index(count,0,index),domElement.attribute("name"));
    traverseNode(domNode,itemModel->index(count+1,0,index));
    }
    @

    this should be:

    @
    if(domElement.tagName() == "folder")
    {
    itemModel->insertRow(count,index);
    QModelIndex folderIdx = itemModel->index(count,0,index);
    itemModel->setData(folderIdx,domElement.attribute("name"));
    traverseNode(domNode,folderIdx);
    }
    @





  • Maybe your XML must look like:
    @<?xml version="1.0" encoding="UTF-8"?>
    <data>
    <item>C:/Qt</item>
    <folder name="Friends">
    <item>C:/Qt1</item>
    <item>C:/Qt2</item>
    </folder>
    </data>@

    And then:
    @if(domElement.tagName() == "folder")
    {
    itemModel->insertRow(count,index);
    itemModel->setData(itemModel->index(count,0,index),domElement.attribute("name"));
    traverseNode(domNode,itemModel->index(count+1,0,index));
    QDomNodeList list = domNode.childNodes(); // and then add all of it to "Friends" item
    }@



  • But, as i understund, it is will not work with folder in folder



  • You can parce it recursively



  • Hellow again!
    I modificated my code and all must work right:
    @void RizekFaster::traverseNode(const QDomNode& node, QModelIndex& index)
    {
    int count = 0;
    QDomNode domNode = node.firstChild();
    while(!domNode.isNull())
    {
    if(domNode.isElement())
    {
    QDomElement domElement = domNode.toElement();
    if(!domElement.isNull())
    {
    if(domElement.tagName() == "folder")
    {
    itemModel->insertRow(count,index);
    QModelIndex newIndex = itemModel->index(count,0,index);
    itemModel->setData(newIndex,domElement.attribute("name",tr("New folder")));
    count++;
    traverseNode(domElement,newIndex);
    }
    if(domElement.tagName() == "item")
    {
    itemModel->insertRow(count,index);
    QModelIndex newIndex = itemModel->index(count,0,index);
    itemModel->setData(newIndex,domElement.text()); //This cant work in the folders
    count++;
    }
    }
    }
    domNode = domNode.nextSibling();
    }
    }@
    But in this @QModelIndex newIndex = itemModel->index(count,0,index);@
    When index is valid and count is valid(0 when there is no 0 rows in this index's table, and 1 when there is 1 rows in the index's table) the newIndex is not valid. Why is it happened?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.