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. xml file
Forum Updated to NodeBB v4.3 + New Features

xml file

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 2 Posters 1.6k Views
  • 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.
  • S siva9111

    HI SIR

    how to display the content in xml file in a text browser in QT

    in console application i gettingthe xml data in command prompt...but in widget application i dont know how to do ...please help me

    my concern is printing xml data in textbrowser(ui) in qt

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #4

    @siva9111 Did you read the link I posted? If you did - what exactly is the problem? If not - then please do, I'm not going to write the code for you...

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    1
    • S Offline
      S Offline
      siva9111
      wrote on last edited by
      #5

      ''below program is in console application''

      #include <QtCore>
      #include <QtXml>
      #include <QDebug>

      void listelements(QDomElement root,QString tagname,QString atttr)
      {

      QDomNodeList items=root.elementsByTagName(tagname);

      qDebug()<<"total items="<<items.count();

      for(int i=0;i<items.count();i++)
      {
      QDomNode itemnode=items.at(i);
      if(itemnode.isElement())
      {
      QDomElement itemle=itemnode.toElement();

      	qDebug()<<itemle.attribute(atttr);
      }
      

      }
      }

      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);
      QDomDocument document;

      QFile file("C:/Users/Administrator/Desktop/siva.xml");
      if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
      {
      qDebug() << "failed to open";
      return -1;
      }
      else
      {
      if(!document.setContent(&file))
      {
      qDebug() << "failed to load";
      return -1;
      }
      file.close();
      }
      QDomElement root=document.firstChildElement();
      listelements(root,"Book","Name");

      qDebug()<<"\r\n more\r\n";

      QDomNodeList books=root.elementsByTagName("Book");
      for(int i=0;i<books.count();i++)
      {
      QDomNode booknode=books.at(i);
      if(booknode.isElement())
      {
      QDomElement book=booknode.toElement();
      qDebug()<<"chapters"<<book.attribute("Name");
      listelements(book,"chapter","Name");
      }
      }

      qDebug() << "finished";

      return a.exec();
      }

      this is the code

      my xml file dtata is

      <?xml version="1.0"?>
      <Books>
      <Book ID="0" Name = "my book 0">
      <chapter ID="0" Name = "my chapter 0"/>
      <chapter ID="1" Name = "my chapter 1"/>
      <chapter ID="2" Name = "my chapter 2"/>
      <chapter ID="3" Name = "my chapter 3"/>
      </Book>
      <Book ID="1" Name = "my book 1">
      <chapter ID="0" Name = "my chapter 0"/>
      <chapter ID="1" Name = "my chapter 1"/>
      <chapter ID="2" Name = "my chapter 2"/>
      <chapter ID="3" Name = "my chapter 3"/>
      </Book>
      </Books>

      i write this on console application

      my concern is same data to be printed on text browser on qt

      jsulmJ 1 Reply Last reply
      0
      • S siva9111

        ''below program is in console application''

        #include <QtCore>
        #include <QtXml>
        #include <QDebug>

        void listelements(QDomElement root,QString tagname,QString atttr)
        {

        QDomNodeList items=root.elementsByTagName(tagname);

        qDebug()<<"total items="<<items.count();

        for(int i=0;i<items.count();i++)
        {
        QDomNode itemnode=items.at(i);
        if(itemnode.isElement())
        {
        QDomElement itemle=itemnode.toElement();

        	qDebug()<<itemle.attribute(atttr);
        }
        

        }
        }

        int main(int argc, char *argv[])
        {
        QCoreApplication a(argc, argv);
        QDomDocument document;

        QFile file("C:/Users/Administrator/Desktop/siva.xml");
        if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
        {
        qDebug() << "failed to open";
        return -1;
        }
        else
        {
        if(!document.setContent(&file))
        {
        qDebug() << "failed to load";
        return -1;
        }
        file.close();
        }
        QDomElement root=document.firstChildElement();
        listelements(root,"Book","Name");

        qDebug()<<"\r\n more\r\n";

        QDomNodeList books=root.elementsByTagName("Book");
        for(int i=0;i<books.count();i++)
        {
        QDomNode booknode=books.at(i);
        if(booknode.isElement())
        {
        QDomElement book=booknode.toElement();
        qDebug()<<"chapters"<<book.attribute("Name");
        listelements(book,"chapter","Name");
        }
        }

        qDebug() << "finished";

        return a.exec();
        }

        this is the code

        my xml file dtata is

        <?xml version="1.0"?>
        <Books>
        <Book ID="0" Name = "my book 0">
        <chapter ID="0" Name = "my chapter 0"/>
        <chapter ID="1" Name = "my chapter 1"/>
        <chapter ID="2" Name = "my chapter 2"/>
        <chapter ID="3" Name = "my chapter 3"/>
        </Book>
        <Book ID="1" Name = "my book 1">
        <chapter ID="0" Name = "my chapter 0"/>
        <chapter ID="1" Name = "my chapter 1"/>
        <chapter ID="2" Name = "my chapter 2"/>
        <chapter ID="3" Name = "my chapter 3"/>
        </Book>
        </Books>

        i write this on console application

        my concern is same data to be printed on text browser on qt

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #6

        @siva9111 Come on - this is really not that hard. You just need to pass the URL to your file to QTextBrowser::setSource and it will load the content of your XML file...
        To get the URL to your file use https://doc.qt.io/qt-5/qurl.html#fromLocalFile

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • S Offline
          S Offline
          siva9111
          wrote on last edited by
          #7

          hi ,
          text files are loading but xml files are not loading by QUrl

          jsulmJ 1 Reply Last reply
          0
          • S siva9111

            hi ,
            text files are loading but xml files are not loading by QUrl

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #8

            @siva9111 said in xml file:

            xml files are not loading by QUrl

            XML files are text files, they should load same way other files do.
            Please show the code where you load the XML file in your text browser.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              siva9111
              wrote on last edited by
              #9

              this is working :
              QUrl url=QUrl::fromLocalFile("C:/Users/Administrator/Desktop/siva.txt");
              ui.textBrowser->setSource(url);

              this not loading:
              QUrl url=QUrl::fromLocalFile("C:/Users/Administrator/Desktop/xmlinfo.xml");
              ui.textBrowser->setSource(url);

              jsulmJ 1 Reply Last reply
              0
              • S siva9111

                this is working :
                QUrl url=QUrl::fromLocalFile("C:/Users/Administrator/Desktop/siva.txt");
                ui.textBrowser->setSource(url);

                this not loading:
                QUrl url=QUrl::fromLocalFile("C:/Users/Administrator/Desktop/xmlinfo.xml");
                ui.textBrowser->setSource(url);

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #10

                @siva9111 said in xml file:

                C:/Users/Administrator/Desktop/xmlinfo.xml

                Does this file exist?
                What does url.isValid() return?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  siva9111
                  wrote on last edited by
                  #11

                  file exist

                  jsulmJ 1 Reply Last reply
                  1
                  • S siva9111

                    file exist

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    @siva9111 What about my second question?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      siva9111
                      wrote on last edited by
                      #13

                      if(url.isValid()){
                      QMessageBox::information(0,"info",url.errorString());
                      }

                      iam executed above code ....messagebox displayed

                      (due to new user iam unable to replying to you sir....its taking 10min time for next reply ...see ur inbox sir)

                      jsulmJ 1 Reply Last reply
                      1
                      • S siva9111

                        if(url.isValid()){
                        QMessageBox::information(0,"info",url.errorString());
                        }

                        iam executed above code ....messagebox displayed

                        (due to new user iam unable to replying to you sir....its taking 10min time for next reply ...see ur inbox sir)

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        @siva9111 Does it work if you use https://doc.qt.io/qt-5/qtextedit.html#setPlainText instead of setSource?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          siva9111
                          wrote on last edited by
                          #15

                          does not work

                          jsulmJ 1 Reply Last reply
                          0
                          • S siva9111

                            does not work

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            @siva9111 said in xml file:

                            does not work

                            Please show the code. setPlainText() should always work...

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            2

                            • Login

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