Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Parse XML file
Qt 6.11 is out! See what's new in the release blog

Parse XML file

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 9.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ruzik
    wrote on last edited by
    #1

    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)!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      RazrFalcon
      wrote on last edited by
      #2

      And what's wrong with it?

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Ruzik
        wrote on last edited by
        #3

        It must look like that:
        !http://s1.hostingkartinok.com/uploads/images/2012/01/6647962d537c44c52f68782f0ac23813.jpg(that)!

        1 Reply Last reply
        0
        • T Offline
          T Offline
          toho71
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            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);
            }
            @

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Ruzik
              wrote on last edited by
              #6

              I do that and get it:
              !http://s1.hostingkartinok.com/uploads/images/2012/01/bc2e0ed4c56e49f4dae844a00440e641.jpg(it)!

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RazrFalcon
                wrote on last edited by
                #7

                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
                }@

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Ruzik
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    RazrFalcon
                    wrote on last edited by
                    #9

                    You can parce it recursively

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Ruzik
                      wrote on last edited by
                      #10

                      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?

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved